<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rex on Medium]]></title>
        <description><![CDATA[Stories by Rex on Medium]]></description>
        <link>https://medium.com/@rexk?source=rss-e489ec23b8d8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*OvGX_bSRv1R5DrDIGjwFPA.png</url>
            <title>Stories by Rex on Medium</title>
            <link>https://medium.com/@rexk?source=rss-e489ec23b8d8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 19:02:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rexk/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Nix and Home Manager at Scale]]></title>
            <link>https://medium.com/@rexk/nix-and-home-manager-at-scale-41a7e2752160?source=rss-e489ec23b8d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/41a7e2752160</guid>
            <category><![CDATA[nix]]></category>
            <dc:creator><![CDATA[Rex]]></dc:creator>
            <pubDate>Sun, 14 May 2023 00:00:34 GMT</pubDate>
            <atom:updated>2023-06-11T06:46:15.176Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wOdc3VooW6VPbtZC" /></figure><h3>Goal</h3><p>We are going to create further hierarchy on both home-manager configurations and shell configurations so that we can separate common configuration out of user specific configurations. This allows us to maintain set of common setup, without compromising user customization.</p><p>We are going to start making these changes from <a href="https://github.com/rexk/dotfiles/tree/nix-flake-install-stage-04">https://github.com/rexk/dotfiles/tree/nix-flake-install-stage-04</a></p><p>For those who are impatient, you can find the final result at <a href="https://github.com/rexk/dotfiles/tree/nix-flake-install-stage-05">https://github.com/rexk/dotfiles/tree/nix-flake-install-stage-05</a></p><h3>home manager configurations</h3><p>In order to provide user specific configurations, we would like to make following changes.</p><ol><li>Update home.nix to import user specific configurations, if exists</li><li>Provide common template for invidual users to get started with user specific configurations</li><li>Provide set of common packages.</li><li>Provide means to override those common packages via user specific configurations.</li></ol><pre>+# This is common homemanager configuration file<br>+# with common setup.<br>+#<br>+# DO NOT MODIFY this file unless, unless<br>+# the change can meant to be supplied to<br>+# others.<br>+#<br>+# For user specific customization, use home.user.nix<br>+{ config, pkgs, lib, ... }:<br> let<br>+  if-exists = f: builtins.pathExists f;<br>+  existing-imports = imports: builtins.filter if-exists imports;<br>   user = import ./user.nix;<br> in<br> {<br>+  imports = existing-imports [<br>+    ./home.user.nix<br>+  ];<br>+<br>   home.username = user.name;<br>   home.homeDirectory = user.homeDir;<br>   home.stateVersion = &quot;22.11&quot;; import ./user.nix;<br> in<br> {</pre><p>Above changes, allow us to import home.user.nix, if exists, otherwise, we are still going to make use home.nix.</p><p>Also note that we are not making any update on home.packages, yet. This is because It is a lot easier to manage list of packages only through home.user.nix, instead of doing it both in home.nix and home.user.nix.</p><p>But we don’t want to just hard code set of common packages in home.user.nix file.</p><p>For that, we are going to create provided-pkgs.nix file</p><pre># This file should be maintained by a team X<br># DO NOT MODIFY directly<br># for local configurations use home.user.nix<br>{ pkgs, lib, ... }:<br>let<br>  removeAll = excludes: all: lib.lists.fold (acc: item: lib.lists.remove item acc) all excludes;<br>  provided-pkgs = with pkgs; [<br>    # Provide org-wide packages here<br>    bashInteractive<br>    jq<br>    curl<br>    git <br>    nodejs-18_x<br>  ];<br>  with-user-pkgs = inst:<br>    let<br>      excludes = inst.excludes;<br>      includes = inst.includes;<br>      without-excludes = removeAll excludes provided-pkgs;<br>    in<br>    lib.lists.unique (provided-pkgs ++ includes);<br>in<br>{<br>  inherit provided-pkgs;<br>  inherit with-user-pkgs;<br>}</pre><p>Then, time to add home.user.nix</p><pre>{ pkgs, lib, ... }:<br>let<br>  provided-pkgs-args = { inherit pkgs; inherit lib; };<br>  provided-pkgs = import ./provided-pkgs.nix provided-pkgs-args;<br>in<br>{<br>  home.packages = with pkgs;<br>    provided-pkgs.with-user-pkgs {<br>      excludes = [ ];<br>      includes = [<br>        gh<br>        exa<br>        lazygit<br>        bat<br>        zellij<br>        unstable.neovim<br>        python311<br>        python311Packages.pip<br>      ];<br>    };<br><br>  programs.starship = {<br>    enable = true;<br>  };<br>}</pre><p>The provided-pkgs.nix provides very neat function for users to manage home-manager packages. For example, let&#39;s say, I would like to use nodejs 14.x over 18.x. A user can now update home.user.nix file into</p><p>Users can now customize further without needing to intricacy of home-manager’s detail. While we would like to commit provided-pkgs.nix, we do not want to check in home.user.nix into our dotfiles, as it would be different per each user. So instead of checking in home.user.nix, we are going to create a file called home.user.nix.tpl, so that users can easily get started by copying content of home.user.nix.tpl as home.user.nix.</p><h3>shell configurations</h3><p>Similar to changes of home-manager configurations, we are going to separate create hierarchy of shell configurations.</p><p>In the previous post, we’ve created hierarchy for each shell that we are going to support in following manner.</p><pre>.<br>├── .bashrc                    # controlled by nix<br>├── .zshrc                     # controlled by nix<br>├── .config <br>│  ├── bash<br>│  │  └── .bashrc              # custom bash config<br>│  ├── zsh<br>│  │  └── .zshrc               # custom zsh config<br>│  ├── fish<br>│  │  ├── config.fish          # controlled by nix<br>|  │  └── config</pre><p>We would like to change them into followings:</p><pre>.<br>├── .bashrc                    # controlled by nix<br>├── .zshrc                     # controlled by nix<br>├── .config <br>│  ├── bash<br>│  │  ├── .bashrc              # common bash config<br>│  │  ├── user.bash            # user bash config<br>│  │  └── secret.bash          # place for API tokens, secrets... etc<br>│  ├── zsh<br>│  │  ├── .zshrc               # common zsh config<br>│  │  ├── user.zsh             # user zsh config<br>│  │  └── secret.zsh           # place for API tokens, secrets... etc<br>│  ├── fish<br>│  │  ├── config.fish          # controlled by nix<br>│  │  ├── config.base.fish     # common fish config<br>│  │  ├── config.user.fish     # user fish config<br>|  │  └── config.secret.fish   #  place for API tokens, secrets... etc</pre><p>Just like what we did in our home.nix file, we just need to update each custom config files to add conditional file import.</p><p>For this to work, we are going to a few lines into .bashrc, .zshrc, and config.base.fish as following:</p><p>.bashrc</p><pre># DO NOT MODIFY this file unless, the change is required<br># for all memebers.<br>#<br># This file provide set of common bash configurations<br># that are useful for all of us.<br><br># set up PATH<br>export PATH=$PATH:$HOME/.config/bin<br><br>if [ -f $HOME/.config/bash/user.bash ]; then<br> source $HOME/.config/bash/user.bash<br>fi<br><br>if [ -f $HOME/.config/bash/secret.bash ]; then<br> source $HOME/.config/bash/secret.bash<br>fi</pre><p>.zshrc</p><pre># DO NOT MODIFY this file unless, the change is required<br># for all memebers.<br>#<br># This file provide set of common zsh configurations<br># that are useful for all of us.<br><br># region: setup<br># TODO:: add more configs<br># endregion <br><br>if [ -f $HOME/.config/zsh/user.zsh ]; then<br>  source $HOME/.config/zsh/user.zsh<br>fi<br><br>if [ -f $HOME/.config/zsh/secret.zsh ]; then<br>  source $HOME/.config/zsh/secret.zsh<br>fi</pre><p>config.base.fish</p><pre># DO NOT MODIFY this file unless, the change is required<br># for all memebers.<br>#<br># This file provide set of common fish configurations<br># that are useful for all of us.<br><br># region: setup<br># TODO:: add more configs<br># endregion <br><br>if test -f $HOME/.config/fish/config.user.fish<br>  source $HOME/.config/fish/config.user.fish<br>end<br><br>if test -f $HOME/.config/fish/config.secret.fish<br>  source $HOME/.config/fish/config.secret.fish<br>end</pre><p>Now, we are ready test these new structure. For a demonstration, let’s add a custom zsh configuration</p><pre>cat &lt;&lt;EOF &gt; ~/.config/zsh/user.zsh<br>echo &quot;My Custom config&quot;<br>EOF<br><br>zsh # loading a new zsh session</pre><p>And you should see that our zsh session prints out</p><pre>My Custom config</pre><h3>Conclusion</h3><p>By supplying a further hierarchy of configurations, we have successfully created a nix set up that can be used to supply common configurations for everyone in an organization, while providing an ability for each users to customize their own environment.</p><p>provided-pkgs.nix file is used to provide common packages that we would like to distribute to everyone in a team, while home.user.nix can be used to provide further customization for an end user.</p><p>Similarly, common shell configurations can be managed by ~/.config/bash/.bashrc, ~/.config/zsh/.zshrc, and~/.config/fish/config.base.fish, while ~/.config/bash/user.bash, ~/.config/zsh/user.zsh, and~/.config/fish/config.user.fish provide place for further customization for an end user, without needing to set up above shell environment using nix.</p><p><em>Originally published at </em><a href="https://rexk.github.io/en/blog/nix-home-manager-at-scale/"><em>https://rexk.github.io</em></a><em> on May 14, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41a7e2752160" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[home-manager and dotfiles]]></title>
            <link>https://medium.com/@rexk/home-manager-and-dotfiles-b1bec0590875?source=rss-e489ec23b8d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/b1bec0590875</guid>
            <category><![CDATA[nix]]></category>
            <dc:creator><![CDATA[Rex]]></dc:creator>
            <pubDate>Wed, 10 May 2023 00:00:57 GMT</pubDate>
            <atom:updated>2023-06-11T06:45:57.375Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vYYMT_WZ1U0EKdZf" /></figure><p>Nix home-manager allows you to control your user environment using nix, providing a declarative approach to managing your dotfiles and system configuration. However, it’s worth noting that you don’t have to use nix for everything. If you choose to, you can still use home-manager to manage your other dotfiles and system configuration while controlling some customization manually. For instance, you can manage a specific configuration file for a particular application manually, while letting home-manager handle everything else. This approach lets you strike a balance between centralized configuration management and controlled customization. By using home-manager to manage most of your configuration files, you can still benefit from its declarative approach, while keeping some level of flexibility for specific cases.</p><p>In this post, we are going to set zsh, fish and bash configuration using nix home-manager. However, the goal is not to use nix for everything.</p><p>The goal is to set up sensible configuration hierarchical structure such that</p><ol><li>Nix configurations that can be shared entire engineering team</li><li>Provides an organization wide configuration</li><li>Does not compromise on user-level customization</li></ol><p>To demonstrate an ability above, we are going to set up, bash, zsh, and fish.</p><h3>Initial bash, zsh, and fish setup.</h3><p>First we are going to set bash, zsh, and fish enabled through home-manager. To do that, ~/.config/home-manager/home.nix must be updated as following.</p><pre>   home.stateVersion = &quot;22.11&quot;;<br><br>   programs.direnv.enable = true;<br>+<br>+  programs.bash.enable = true;<br>+  programs.zsh.enable = true;<br>+  programs.fish.enable = true;<br>}</pre><p>Once the file is saved, we need to apply the home-configuration. We’ve alraedy created the handy script to do that at $HOME/.config/bin/hms, so let&#39;s call that.</p><pre>$HOME/.config/bin/hms<br><br># Will print followings<br>Starting Home Manager activation<br>Activating checkFilesChanged<br>Activating checkLinkTargets<br>Existing file &#39;/home/rexk/.bashrc&#39; is in the way of &#39;/nix/store/pzf4zq0ls1lj7jqfch0yf114mw8gn3hj-home-manager-files/.bashrc&#39;<br>Please move the above files and try again or use &#39;home-manager switch -b backup&#39; to back up existing files automatically.</pre><p>Oh, of course. home-manager wants to create symlink on ~/.bashrc, ~/.zshrc, and ~/.config/fish/config.fish, but we usuall already have our own file there. Since we are using nix flake to control home-manager rather than usual home-manager route, we can&#39;t use home-manager switch -b backup command. So let&#39;s back everything up ourselves</p><pre>mv ~/.bashrc ~/.bashrc.bak<br>mv ~/.zshrc ~/.zshrc.bak<br>mv ~/.config/fish/config.fish ~/.config/fish/config.fish.bak</pre><p>Since, this might be common for many people, let’s make a script that people can use. Let’s call this script backup-original-rc-files and place it under ~/.config/bin</p><pre>#!/usr/bin/env bash<br><br>backup_file_if_exists() {<br>  local file=&quot;$1&quot;<br><br>  if [ -f $file ];<br>  then<br>    mv -f $file $file.bak <br>  fi<br>}<br><br>backup_file_if_exists ~/.bashrc<br>backup_file_if_exists ~/.zshrc<br>backup_file_if_exists ~/.config/fish/config.fish<br><br>case &quot;$(uname -s)&quot; in<br>  Darwin)<br>    # for MacOS as we would be installing <br>    # nix-darwin as well<br>    # it is good idea to back up the original <br>    sudo backup_file_if_exists /etc/zshrc<br>    sudo backup_file_if_exists /etc/bashrc<br>    ;;<br>esac</pre><p>Once all files are backed up, let’s try again</p><pre>$HOME/.config/bin/hms<br><br>tarting Home Manager activation<br>Activating checkFilesChanged<br>Activating checkLinkTargets<br>Activating writeBoundary<br>Activating installPackages<br>replacing old &#39;home-manager-path&#39;<br>installing &#39;home-manager-path&#39;<br>Activating linkGeneration<br>Cleaning up orphan links from /home/rexk<br>Creating profile generation 29<br>Creating home file links in /home/rexk<br>Activating onFilesChange<br>Activating reloadSystemd</pre><p>Ok, that’s better.</p><p>We can try to configure all of our shell with nix, but I would rather want to keep most of our shell configurations in our usual shell file, despite we are making our nix environment impure.</p><p>So, let’s give a bit more changes on to our ~/.config/home-manager/home.nix file.</p><pre>   home.stateVersion = &quot;22.11&quot;;<br><br>   programs.direnv.enable = true;<br>+<br>+  programs.bash = {<br>+    enable = true;<br>+    initExtra = &quot;<br>+if [ -f $HOME/.config/bash/.bashrc ];<br>+then<br>+  source $HOME/.config/bash/.bashrc<br>+fi<br>+&quot;;<br>+  };<br>+  programs.fish = {<br>+    enable = true;<br>+    initExtra = &quot;<br>+if test -f $HOME/.config/fish/config.base.fish;<br>+then<br>+  source $HOME/.config/fish/config.base.fish<br>+fi<br>+&quot;;<br>+  };<br>+  programs.zsh = {<br>+    enable = true;<br>+    initExtra = &quot;<br>+if [ -f $HOME/.config/zsh/.zshrc ];<br>+then<br>+  source $HOME/.config/zsh/.zshrc<br>+fi<br>+  };</pre><p>Instead of configuring bunch of things through nix, we are simply going to delegate further configurations into respectful rc files. This allows us to keep using familiar configuration without needing to learn more on nix and it’s home-manager configuration structure.</p><h3>Referencess</h3><p>All file changes can be found at: <a href="https://github.com/rexk/dotfiles/tree/nix-flake-install-stage-02">https://github.com/rexk/dotfiles/tree/nix-flake-install-stage-02</a></p><p><em>Originally published at </em><a href="https://rexk.github.io/en/blog/home-manager-and-dotfiles/"><em>https://rexk.github.io</em></a><em> on May 10, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b1bec0590875" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Setting up home-manager with nix-direnv]]></title>
            <link>https://medium.com/@rexk/setting-up-home-manager-with-nix-direnv-2a922af8a0a1?source=rss-e489ec23b8d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a922af8a0a1</guid>
            <category><![CDATA[nix]]></category>
            <dc:creator><![CDATA[Rex]]></dc:creator>
            <pubDate>Sun, 09 Apr 2023 00:00:52 GMT</pubDate>
            <atom:updated>2023-06-11T06:45:41.938Z</atom:updated>
            <content:encoded><![CDATA[<h3>Goal</h3><p>We’ve set up nix and home-manager with flake from our the previous post. In this post, we are going to further customize our dotfiles, and then integrate nix-direnv using home-manager so that we can start having reproducible dev environment per directory.</p><p>For those of you that are impatient, a full snippets can be found at</p><ul><li><a href="https://github.com/rexk/home-manager-direnv-example/tree/nodejs-example">https://github.com/rexk/home-manager-direnv-example/tree/nodejs-example</a></li><li><a href="https://github.com/rexk/dotfiles/tree/nix-flake-install-stage-02">https://github.com/rexk/dotfiles/tree/nix-flake-install-stage-02</a></li></ul><h3>Before getting started</h3><p>Before getting started, let’s add a convenient shell script so that we can interact with our dotfiles bare repo easier.</p><p>Add following script at: ~/.config/bin/dit</p><pre>#!/usr/bin/env sh<br><br># ------------------------------------------------<br># dit - short hand for git for dotfiles<br># ------------------------------------------------<br># As we control dotfiles as git&#39;s bare repo, <br># it is cumbersome to type out whole work-tree<br># git command to interact with our dotfiles folder<br>#<br># dit command gives us easy access to control out dotilfes<br># without deviating too much from our `git` command flow<br><br><br>CONFIG_DIR=${XDG_CONFIG_HOME:=$HOME/.config}<br>git --git-dir=$HOME/.dotfiles.git --work-tree=$CONFIG_DIR &quot;$@&quot;</pre><h3>direnv and nix-direnv</h3><p>direnv let me control directory level dev environment setup with .envrc file. But I would like to use this with nix&#39;s flake. Luckily there is a community project laready exists with nix and direnv integration, nix-direnv.</p><p>With home-manager, it is really simple to integrate nix-direnv with my current system.</p><pre>@@ -5,5 +5,7 @@ in<br>   home.username = user.name;<br>   home.homeDirectory = user.homeDir;<br>   home.stateVersion = &quot;22.11&quot;;<br>+<br>+  programs.direnv.enable = true;<br> }</pre><p>After the file is updated, now we need to run our hms (home-manager switch) command.</p><p>We can verify our installation by running direnv from the terminal.</p><pre>direnv --version<br>2.32.1</pre><p>Now that, we have dirnev installed, it is time for us to test out our direnv setup.</p><p>I would like to set up a nodejs project using express NodeJs version with 18.x LTS.</p><ol><li>Create a project directory</li><li>Add flake.nix</li><li>Update flake.nix to pull nodejs 18.x</li><li>Add .envrc</li><li>Execute direnv allow</li></ol><pre>mkdir -p nodejs-18-example<br>cd nodejs-18-example</pre><p>Then, we are going to add following flake.nix at the root of nodejs-18-example</p><pre>{<br>  description = &quot;nodejs-18-example-flake&quot;;<br><br>  inputs = {<br>    nixpkgs.url = &quot;github:NixOS/nixpkgs/nixos-22.11&quot;;<br>    flake-utils.url = &quot;github:numtide/flake-utils&quot;;<br>  };<br><br>  outputs = { nixpkgs, flake-utils, ... }:<br>    let<br>      utils = flake-utils;<br>    in<br>    utils.lib.eachDefaultSystem (system:<br>      let<br>        pkgs = nixpkgs.legacyPackages.${system};<br>      in<br>      {<br>        formatter = pkgs.nixpkgs-fmt;<br><br>        devShell = pkgs.mkShell {<br>          nativeBuildInputs = with pkgs; [<br>            nodejs-18_x<br>          ];<br>        };<br>      }<br>    );<br>}</pre><p>Before we proceed further, flake.nix file must be committed into git.</p><pre>git add flake.nix<br>git commit -am &quot;Add flake.nix&quot;</pre><p>Once flake.nix file is committed, we are going to add .envrc file with the following content:</p><pre>use flake</pre><p>Once both files are successfully added, we are going to see following error messages:</p><pre>direnv: error /home/rexk/rexk-workspace/home-manager-direnv-example/nodejs-18-express/.envrc is blocked. Run `direnv allow` to approve its content</pre><p>Run direnv allow so that direnv can do its job.</p><p>Now, nix and direnv will do its magic, and download nodejs for us.</p><pre>node --version<br>v18.14.1</pre><p>As long as flake.lock file and flake.nix file does not change, nodejs-18-express will alawys use same nodejs versions no matter who enter this project. Neat</p><p><em>Originally published at </em><a href="https://rexk.github.io/en/blog/nix-home-manager-direnv/"><em>https://rexk.github.io</em></a><em> on April 9, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a922af8a0a1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nix and Home manager setup]]></title>
            <link>https://medium.com/@rexk/nix-and-home-manager-setup-904c7112a92b?source=rss-e489ec23b8d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/904c7112a92b</guid>
            <category><![CDATA[fish]]></category>
            <category><![CDATA[bash]]></category>
            <category><![CDATA[zsh]]></category>
            <category><![CDATA[nix]]></category>
            <dc:creator><![CDATA[Rex]]></dc:creator>
            <pubDate>Sun, 02 Apr 2023 00:00:10 GMT</pubDate>
            <atom:updated>2023-06-11T06:37:42.577Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*7qdaEmKgc8zhp-X-" /></figure><h3>Goal</h3><p>In this post, we will set up nix and home-manager configuration that can be used in any development machine. At the end, I would like to achieve following goals:</p><ol><li>Nix and Home Manager setup that can be easily transferred into different machines.</li><li>Replace 99% of my usual homebrew or apt usage with home manager.</li></ol><p>For those of you that are impatient, a full snippets can be found at <a href="https://github.com/rexk/dotfiles/tree/nix-flake-install-stage-01">https://github.com/rexk/dotfiles/tree/nix-flake-install-stage-01</a></p><h3>Installing Nix</h3><p>Nix’s official installer is more then enough to get started.</p><p><a href="https://nixos.org/download.html#download-nix">https://nixos.org/download.html#download-nix</a></p><p>I won’t go too much detail on each installation options. I chose its recommended installation instructions.</p><h3>Setting up dotfiles git repository</h3><p>In order for the setup to be (somewhat) universal, I need way to share my configurations between different machines. For that, we are going to track and share dotfiles with git.</p><p>There are numerous articles explaining on how we can achieve this. My setup is particularly inspired by two articles:</p><p>In summary, our dotfiles git repository will have following characteristics:</p><ol><li>All files will be ignored with .gitignore to prevent accidental commit on sensitive information</li><li>Git directory will be placed different from its work tree. This prevents nix’s flake honoring gitignore file.</li></ol><p>Detailed instructions how to set up this dotfiles repository will be covered on a different post.</p><p>For now, consider that we are going to work on $HOME/.config directory</p><p>Before we set up anything add following file to the $HOME/.config directory</p><pre>cat &lt;&lt;EOF&gt;$HOME/.config/.gitignore<br>*<br>!.gitignore<br><br>EOF</pre><h3>Installing Nix-Darwin</h3><p>While you don’t have to, it is good idea to install nix-darwin, if you are using a mac. We will go over nix-darwin setup using flake, in an another post.</p><h3>Installing Home Manager with flake</h3><p>If we follow instructions from the official home manager’s manual, we end up with following flake file.</p><pre># flake.nix<br>{<br>  description = &quot;Home Manager configuration of Jane Doe&quot;;<br><br>  inputs = {<br>    # Specify the source of Home Manager and Nixpkgs.<br>    nixpkgs.url = &quot;github:nixos/nixpkgs/nixos-22.11&quot;;<br>    home-manager = {<br>      url = &quot;github:nix-community/home-manager/release-22.11&quot;;<br>      inputs.nixpkgs.follows = &quot;nixpkgs&quot;;<br>    };<br>  };<br><br>  outputs = { nixpkgs, home-manager, ... }:<br>    let<br>      system = &quot;x86_64-linux&quot;;<br>      pkgs = nixpkgs.legacyPackages.${system};<br>    in {<br>      homeConfigurations.jdoe = home-manager.lib.homeManagerConfiguration {<br>        inherit pkgs;<br><br>        # Specify your home configuration modules here, for example,<br>        # the path to your home.nix.<br>        modules = [<br>          ./home.nix<br>        ];<br><br>        # Optionally use extraSpecialArgs<br>        # to pass through arguments to home.nix<br>      };<br>    };<br>}<br><br># home.nix<br>{<br>  # Set following two fields in your home.user.nix properly<br>  home.username = &quot;jdoe&quot;;<br>  home.homeDirectory = &quot;/home/jdoe&quot;;<br>  home.stateVersion = &quot;22.11&quot;;<br>}</pre><p>The installation instructs to update user specific information on home.nix and flake.nix. However, this isn’t enough for our use case. We need to be able to use same flake file on our each dev machines. Unfortunately, You might have different user names for each dev machines, like I do. Not only I have different user name, I have different system configurations for each machine.</p><p>For the flake.nix and home.nix to be used on each of my machine, I need to abstract user specific information out of flake.nix, and home.nix.</p><p>For that, I’ve made following changes.</p><pre> {<br>-  description = &quot;Home Manager configuration of Jane Doe&quot;;<br>+  description = &quot;Home Manager configuration&quot;;<br> <br>   inputs = {<br>     # Specify the source of Home Manager and Nixpkgs.<br>-    nixpkgs.url = &quot;github:nixos/nixpkgs/nixos-22.11&quot;;<br>+    nixpkgs.url = &quot;github:NixOS/nixpkgs/nixos-22.11&quot;;<br>+    nixpkgs-unstable.url = &quot;github:NixOs/nixpkgs/nixos-unstable&quot;;<br>     home-manager = {<br>       url = &quot;github:nix-community/home-manager/release-22.11&quot;;<br>       inputs.nixpkgs.follows = &quot;nixpkgs&quot;;<br>     };<br>+    flake-utils.url = &quot;github:numtide/flake-utils&quot;;<br>   };<br> <br>-  outputs = { nixpkgs, home-manager, ... }:<br>+  outputs = { nixpkgs, flake-utils, home-manager, nixpkgs-unstable, ... }:<br>     let<br>-      system = &quot;x86_64-linux&quot;;<br>-      pkgs = nixpkgs.legacyPackages.${system};<br>-    in {<br>-      homeConfigurations.jdoe = home-manager.lib.homeManagerConfiguration {<br>-        inherit pkgs;<br>+      utils = flake-utils;<br>+      user = import ./user.nix;<br>+    in<br>+    utils.lib.eachDefaultSystem (system:<br>+      let<br>+        stable-pkgs = nixpkgs.legacyPackages.${system};<br>+        unstable-pkgs = nixpkgs-unstable.legacyPackages.${system};<br>+        pkgs = stable-pkgs // {<br>+          # provides alias for all ustable pkgs<br>+          unstable = unstable-pkgs;<br> <br>-        # Specify your home configuration modules here, for example,<br>-        # the path to your home.nix.<br>-        modules = [<br>-          ./home.nix<br>-        ];<br>+          starship = unstable-pkgs.starship;<br>+          git = unstable-pkgs.git;<br>+        };<br>+      in<br>+      {<br>+        formatter = pkgs.nixpkgs-fmt;<br> <br>-        # Optionally use extraSpecialArgs<br>-        # to pass through arguments to home.nix<br>-      };<br>-    };<br>+        packages = {<br>+          homeConfigurations.${user.name} = home-manager.lib.homeManagerConfiguration {<br>+            inherit pkgs;<br>+<br>+            # Specify your home configuration modules here, for example,<br>+            # the path to your home.nix.<br>+            modules = [<br>+              ./home.nix<br>+            ];<br>+<br>+            # Optionally use extraSpecialArgs<br>+            # to pass through arguments to home.nix<br>+            extraSpecialArgs = {<br>+              inherit pkgs;<br>+            };<br>+          };<br>+        };<br>+      });<br> }</pre><pre>+let<br>+  user = import ./user.nix;<br> {<br>-  home.username = &quot;jdoe&quot;;<br>-  home.homeDirectory = &quot;/home/jdoe&quot;;<br>+  home.username = user.name;<br>+  home.homeDirectory = user.homeDir;<br>   home.stateVersion = &quot;22.11&quot;;<br> }</pre><p>where user.nix is rendered output of user.nix.tpl</p><pre># user.nix.tpl<br>{<br>  name = &quot;$USER&quot;;<br>  hostname = &quot;$(hostname)&quot;;<br>  homeDir = &quot;$HOME&quot;;<br>}</pre><p>Above can be rendered with following script:</p><pre>#!/usr/bin/env sh<br><br>#-------------------------------------------------<br># render-user-nix<br>#-------------------------------------------------<br># render user specific nix files for home-manager<br># If the current environment is darwin, <br># we will also render configurations for nix-darwin (osx)<br><br>render_user_nix() {<br>  local file=&quot;$1&quot;<br>  local dest=&quot;$2&quot;<br>  if [ -f &quot;$file&quot; ];<br>  then<br>    local tmp=&quot;/tmp/$(basename $1)&quot;<br>    # substitutes sh experssions<br>   (echo &quot;cat &lt;&lt;EOF&quot;; cat $file; echo EOF) | sh &gt; $tmp<br>    cp $tmp $dest<br>  fi<br>}<br><br>CONFIG_DIR=${XDG_CONFIG_HOME:-$HOME/.config}<br><br>render_user_nix $CONFIG_DIR/home-manager/user.nix.tpl $CONFIG_DIR/home-manager/user.nix<br><br>case &quot;$(uname -s)&quot; in<br>  Darwin)<br>    render_user_nix $CONFIG_DIR/nix-darwin/user.nix.tpl $CONFIG_DIR/nix-darwin/user.nix<br>    ;;<br>esac<br><br># no need to keep the function<br>unset render_user_nix</pre><p>Since I need this script for my other machines, I am going to place this script under ~/.config/bin folder.</p><p>Additionally, I’ve pulled <a href="https://github.com/numtide/flake-utils">https://github.com/numtide/flake-utils</a> into my flake. This lets me define flake configuration per each of systems that I use.</p><h3>Applying home configuration</h3><p>Since we are using flake installation, usual home-manager command is not available in our path. We need to explicitly run nix run command.</p><p>If my user name is jdoe, the command will look like the following:</p><pre>nix run /home/jdoe/home-manager#homeConfigurations.jdoe.activationPackage</pre><p>However, I want to make these configurations work on any machine, so let’s provide another script for home-manager activation</p><pre>#!/usr/bin/env sh<br><br>set -e;<br><br>#-------------------------------------------------<br># hms<br>#-------------------------------------------------<br># aliasing &quot;home-manager switch&quot; command for flake<br>CONFIG_DIR=${XDG_CONFIG_HOME:=$HOME/.config}<br>/nix/var/nix/profiles/default/bin/nix \<br>  --extra-experimental-features &quot;nix-command flakes&quot; \<br>  run $CONFIG_DIR/home-manager#homeConfigurations.\&quot;$USER\&quot;.activationPackage</pre><h3>Final file tree</h3><pre>├── bin<br>│  ├── render-user-nix<br>│  └── hms <br>└── home-manager<br>   ├── flake.nix<br>   ├── flake.lock<br>   ├── home.nix<br>   ├── user.nix &lt;-- generated by render-user-nix. Not checked in<br>   └── user.nix.tpl</pre><h3>References</h3><ul><li>Nix Install: <a href="https://nixos.org/download.html">https://nixos.org/download.html</a></li><li>Home Manager Manual: <a href="https://rycee.gitlab.io/home-manager/">https://rycee.gitlab.io/home-manager/</a></li><li><a href="https://drewdevault.com/2019/12/30/dotfiles.html">https://drewdevault.com/2019/12/30/dotfiles.html</a></li><li><a href="https://www.atlassian.com/git/tutorials/dotfiles">https://www.atlassian.com/git/tutorials/dotfiles</a></li></ul><p><em>Originally published at </em><a href="https://rexk.github.io/en/blog/nix-home-manager-flake-setup/"><em>https://rexk.github.io</em></a><em> on April 2, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=904c7112a92b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nix, Home Manager, and Flake]]></title>
            <link>https://medium.com/@rexk/nix-home-manager-and-flake-bb4cb5ef4b1a?source=rss-e489ec23b8d8------2</link>
            <guid isPermaLink="false">https://medium.com/p/bb4cb5ef4b1a</guid>
            <category><![CDATA[nix]]></category>
            <dc:creator><![CDATA[Rex]]></dc:creator>
            <pubDate>Sat, 01 Apr 2023 00:00:14 GMT</pubDate>
            <atom:updated>2023-06-11T06:23:31.909Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hUDz-yNNa2Kiy55O" /></figure><h3>Motivation</h3><p>As a developer, (unless you are very fortunate) we work with many different languages, frameworks and tools. Even if one is working on same framework with same language, you are bound to work with different versions of the language or a framework (or both) depending on a project you are assigned to. This creates interesting challenges on identifying and configuring a right tool for a right job.</p><p>There are many tools that could solve the problem, but I eventually settled on nix, flake, home-manager. and dotfiles as git repository.</p><p>I am going to go over alternative tools, and explain why I settled with nix.</p><h3>Other tools</h3><h3>homebrew or os specific package manager.</h3><p>In OSX, package managers like homebrew, apt, apk, yum, and others that come with operating systems are really convenient when setting up one version of software that applies to the entire system. However, I develop and manage programs that are distributed across hundreds of git repositories. These programs are not created overnight but accumulate over time in each repository.</p><p>Therefore, older programs that I made may not work with different versions of languages and build tools. Especially now that new versions of languages like Java or NodeJS are released every six months, version diversity is increasing.</p><p>Also, because the programming languages and their versions used are different, even though each repository has instructions for installing and setting up development tools in their README.md files, it is common for them to not work properly when I try to run them again after a few years.</p><h3>pyenv, nvm, jenv</h3><p>I started using a program version management tool because I couldn’t keep up with all the package management programs. Initially, I started with environment configuration tools for specific languages, such as pyenv for Python, nvm for Node.js, and jenv for Java. However, as I manage too many repositories, these configuration tools also became harder to use over time as they are updated, and their usage and provided language versions change.</p><p>Furthermore, each program has different installation methods and bash configuration methods, so teaching all my colleagues the installation process was very time-consuming.</p><p>While searching for a tool to solve all these problems with one program, I came across asdf-vm.</p><h3>What about Docker?</h3><p>When developing a software, I highly value fast feedback between my code and the result I produce. For that, I rely on my IDE or editor’s fast response on various things (syntax highlighting, syntax error, type error, debugger, …)</p><p>Hosting my code in docker and piping into my local development setup always has been sub-optimal, thus docker was never my consideration.</p><p>Problems</p><ul><li>Difficult to maintain documentations for all tools</li><li>Install instruction for each tool</li><li>Environment set up instructions (bash, zsh, fish… etc)</li><li>Only limited</li><li>Difficult to maintain plugin versions synced for all developers</li></ul><h3>So Nix can solve all of the problems?</h3><p>Well, not 100%, but very close to it. I am happy with it at the moment</p><h3>What is Nix?</h3><p>Nix is a package manager and a language used to manage software dependencies and builds. It’s different from other package managers because it uses a purely functional approach, which means that packages are isolated and have their own dependencies. This makes it easy to create consistent and reproducible software environments.</p><p>Nix also has a powerful language for describing software builds, which allows developers to specify exact versions and configurations of dependencies. Nix is commonly used in research and scientific computing to ensure that experiments are reproducible, and in production environments to maintain consistent and reliable software versions.</p><p>Overall, Nix is a useful tool for managing software dependencies and builds with a focus on reproducible and maintainability.</p><p>For more detail information, checkout the official website. <a href="https://nixos.org/">https://nixos.org/</a></p><h3>So why use flake?</h3><p>Nix Flake is a recent addition to the Nix package manager that aims to simplify and streamline the management of Nix packages and configurations. It allows developers to define all their dependencies, configurations, and build processes in a single file called a “flake,” which can be easily version-controlled and shared across different projects.</p><p>Nix’s flake always comes with set of two files at minimum. flake.nix and flake.lock file. flake.nix &#39;s content includes declaration of inputs and outputs.</p><pre>{<br>  description = &quot;Home Manager for Nix&quot;;<br><br>  inputs.nixpkgs.url = &quot;github:nixos/nixpkgs/nixos-unstable&quot;;<br>  inputs.utils.url = &quot;github:numtide/flake-utils&quot;;<br><br>  outputs = { self, nixpkgs, utils, ... }:<br>}</pre><pre><br>  &quot;nodes&quot;: {<br>    &quot;nixpkgs&quot;: {<br>      &quot;locked&quot;: {<br>        &quot;lastModified&quot;: 1677932085,<br>        &quot;narHash&quot;: &quot;sha256-+AB4dYllWig8iO6vAiGGYl0NEgmMgGHpy9gzWJ3322g=&quot;,<br>        &quot;owner&quot;: &quot;nixos&quot;,<br>        &quot;repo&quot;: &quot;nixpkgs&quot;,<br>        &quot;rev&quot;: &quot;3c5319ad3aa51551182ac82ea17ab1c6b0f0df89&quot;,<br>        &quot;type&quot;: &quot;github&quot;<br>      },<br>      &quot;original&quot;: {<br>        &quot;owner&quot;: &quot;nixos&quot;,<br>        &quot;ref&quot;: &quot;nixos-unstable&quot;,<br>        &quot;repo&quot;: &quot;nixpkgs&quot;,<br>        &quot;type&quot;: &quot;github&quot;<br>      }<br>    },<br>    &quot;root&quot;: {<br>      &quot;inputs&quot;: {<br>        &quot;nixpkgs&quot;: &quot;nixpkgs&quot;,<br>        &quot;utils&quot;: &quot;utils&quot;<br>      }<br>    },<br>    &quot;utils&quot;: {<br>      &quot;locked&quot;: {<br>        &quot;lastModified&quot;: 1676283394,<br>        &quot;narHash&quot;: &quot;sha256-XX2f9c3iySLCw54rJ/CZs+ZK6IQy7GXNY4nSOyu2QG4=&quot;,<br>        &quot;owner&quot;: &quot;numtide&quot;,<br>        &quot;repo&quot;: &quot;flake-utils&quot;,<br>        &quot;rev&quot;: &quot;3db36a8b464d0c4532ba1c7dda728f4576d6d073&quot;,<br>        &quot;type&quot;: &quot;github&quot;<br>      },<br>      &quot;original&quot;: {<br>        &quot;owner&quot;: &quot;numtide&quot;,<br>        &quot;repo&quot;: &quot;flake-utils&quot;,<br>        &quot;type&quot;: &quot;github&quot;<br>      }<br>    }<br>  },<br>  &quot;root&quot;: &quot;root&quot;,<br>  &quot;version&quot;: 7<br>}</pre><p>Just like package-lock.json, flake commits snapshot of inputs. This can eliminate most of our work-on-my-machine problem.</p><h3>So now what?</h3><p>On the next post, I will go over the basic set up, then expand the set up over to be shared with others.</p><p><em>Originally published at </em><a href="https://rexk.github.io/en/blog/nix-and-flake/"><em>https://rexk.github.io</em></a><em> on April 1, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bb4cb5ef4b1a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>