blog/content/post/use-home-manager-instead-of-nix-env.md
2022-09-02 11:00:18 +05:30

3.4 KiB

title date lastmod draft keywords description tags categories author comment toc autoCollapseToc postMetaInFooter hiddenFromHomePage contentCopyright reward mathjax mathjaxEnableSingleDollar mathjaxEnableAutoNumber hideHeaderAndFooter flowchartDiagrams sequenceDiagrams
Use home-manager Instead of nix-env 2022-09-01T12:47:47+05:30 2022-09-01T12:47:47+05:30 false
nix home-manager nix-env
false true false true false false false false false false false
enable options
false
enable options
false

home-manager allows reprodicuble builds compared to nix-env. You can port your configuration to any machine and expect it to work the same.

This post assumes you are on a non-NixOS Linux system.

Getting nix

Nix can be installed via a simple shell script provided on their website. Just use your judgement and follow the prompts.

Installing home-manager

The configuration is stored in ~/.config/nixpkgs/home.nix. Run home-manager switch to have changes take effect.

Export NIX_PATH to your shell

export NIX_PATH=$HOME/.nix-defexpr/channels:/nix/var/nix/profiles/per-user/root/channels${NIX_PATH:+:$NIX_PATH}

See issue#2033 and Nix discourse.

Now add unstable nix channel

 nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager

Or latest stable (as of writing this post) 22.05, if you prefer it

 nix-channel --add https://github.com/nix-community/home-manager/archive/release-22.05.tar.gz home-manager

And update

nix-channel --update

Install Home Manager

nix-shell '<home-manager>' -A install 

Home Manager requires your shell to source ~/.nix-profile/etc/profile.d/hm-session-vars.sh. It can be easily done by setting programs.bash.enable = true; (of course, here bash is just an example). This way, Home Manager will manage your shell configuration. However, you will need to migrate your bashrc configurations over to Home Manager, which might take some time. An easier way out would be to move your existing bashrc and have Home Manager use that.

{ pkgs, ...}: {
  programs.bash = {
    enable = true;
    bashrcExtra = ''
      . ~/oldbashrc
    '';
  };
}

Since we are on a non-NixOS Linux system, Home Manager can automatically set some environment variables that will ease the usage of software installed with nix.

{pkgs, ...}: {
  targets.genericLinux.enable = true;
}

Now Home Manager is ready to use. There are many ways to install and configure packages using nix. Putting them here will make this post quite exhaustive. Check the links in the references as they go deeper and have up-to-date docs and examples.

References

NixOS Wiki - Home Manager

Home Manager Manual