Home Manager: Difference between revisions
m →Using Home Manager as a declarative version of nix-env: enhance title |
m Fix probable typo |
||
| (3 intermediate revisions by 2 users not shown) | |||
| Line 2: | Line 2: | ||
* install software declaratively in your user profile, rather than using nix-env | * install software declaratively in your user profile, rather than using nix-env | ||
* manage dotfiles in the home directory of your user. | * manage dotfiles in the home directory of your user. | ||
Home Manager has many [https://nix-community.github.io/home-manager/options.xhtml options], which can look daunting at first, but most of those options only boil down to creating some dotfiles and installing some software in a | Home Manager has many [https://nix-community.github.io/home-manager/options.xhtml options], which can look daunting at first, but most of those options only boil down to creating some dotfiles and installing some software in a similar way to nix-env. | ||
{{Note|Before attempting to use Home Manager [https://github.com/rycee/home-manager#words-of-warning please read the warning].}} | {{Note|Before attempting to use Home Manager [https://github.com/rycee/home-manager#words-of-warning please read the warning].}} | ||
| Line 16: | Line 16: | ||
Your configuration is stored in <code>~/.config/home-manager/home.nix</code>. Each time you modify it, rerun <code>home-manager switch</code> for changes to have effect. | Your configuration is stored in <code>~/.config/home-manager/home.nix</code>. Each time you modify it, rerun <code>home-manager switch</code> for changes to have effect. | ||
To work correctly, | To work correctly, Home Manager needs your shell to source <code>~/.nix-profile/etc/profile.d/hm-session-vars.sh</code>. The most convenient way to do so is to have Home Manager manage your whole shell configuration, eg <code>programs.bash.enable = true;</code> or <code>programs.zsh.enable = true;</code>. But in this case your whole bashrc is managed with Home Manager: the years of customization you accumulated in your former .bashrc must be migrated to Home Manager options, which may take some time. The quick and dirty way to do the migration is to move your bashrc to some other location and source it from Home Manager: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ pkgs, ...}: { | { pkgs, ...}: { | ||
| Line 50: | Line 50: | ||
It can either be incorporated in <code>/etc/nixos/configuration.nix</code> or be placed in a standalone file and imported in configuration.nix: <code>imports = [ ./thefile.nix ]</code>. | It can either be incorporated in <code>/etc/nixos/configuration.nix</code> or be placed in a standalone file and imported in configuration.nix: <code>imports = [ ./thefile.nix ]</code>. | ||
Whenever you change you | Whenever you change you Home Manager configuration, you must rerun <code>nixos-rebuild switch</code>. With this method, changing the configuration of an unprivileged user requires to run a command as root. | ||
=== Usage as a NixOS module in a Flake === | === Usage as a NixOS module in a Flake === | ||
| Line 75: | Line 75: | ||
modules = [ | modules = [ | ||
./configuration.nix | ./configuration.nix | ||
home-manager.nixosModules. | home-manager.nixosModules.default | ||
{ | { | ||
home-manager | home-manager = { | ||
useGlobalPkgs = true; | |||
useUserPackages = true; | |||
users.<USERNAME> = ./home.nix; # replace <USERNAME> with your actual username | |||
}; | |||
} | } | ||
]; | ]; | ||
| Line 87: | Line 89: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Here's an example of | Here's an example of Home Manager configuration in ./home.nix | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ config, pkgs, ... }: | { config, pkgs, ... }: | ||
| Line 106: | Line 108: | ||
Of course you'll probably want to keep more stuff in there than just a state version, but the state version is required. | Of course you'll probably want to keep more stuff in there than just a state version, but the state version is required. | ||
The downside to doing it this way over the User config is that you have to do a full system rebuild; the | The downside to doing it this way over the User config is that you have to do a full system rebuild; the Home Manager config is part of the full system, and so must be built as root or at least a trusted user. | ||
== Usage == | == Usage == | ||
| Line 115: | Line 117: | ||
<code>nix-env -i jdk8</code>, running <code>nix-env --upgrade</code> upgrades java to 10 despite the fact that we initially explicitly requested java 8. | <code>nix-env -i jdk8</code>, running <code>nix-env --upgrade</code> upgrades java to 10 despite the fact that we initially explicitly requested java 8. | ||
Installing software with Home | Installing software with Home Manager avoids this problem: | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
{ pkgs, ...}: { | { pkgs, ...}: { | ||
| Line 122: | Line 124: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
It is a perfectly valid use case for | It is a perfectly valid use case for Home Manager to only install software with <code>home.packages</code> without managing dotfiles at all. | ||
=== Usage on non-NixOS Linux === | === Usage on non-NixOS Linux === | ||
| Line 139: | Line 141: | ||
programs.git = { | programs.git = { | ||
enable = true; | enable = true; | ||
settings = { | |||
user = { | |||
name = "my_git_username"; | |||
email = "my_git_username@gmail.com"; | |||
}; | |||
}; | |||
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 157: | Line 163: | ||
{{Note|In both ways ("source" and "text"), the symlink points to a target in /nix/store. The difference is that, "source" would overwrite the content of the dot file, while "text" would add the text to the dot file that may also be affected in other places.}} | {{Note|In both ways ("source" and "text"), the symlink points to a target in /nix/store. The difference is that, "source" would overwrite the content of the dot file, while "text" would add the text to the dot file that may also be affected in other places.}} | ||
You have the whole list of the options available in | You have the whole list of the options available in Home Manager [https://nix-community.github.io/home-manager/options.xhtml here] | ||
=== Examples === | === Examples === | ||
| Line 167: | Line 173: | ||
== FAQ == | == FAQ == | ||
=== I cannot set GNOME or Gtk themes via | === I cannot set GNOME or Gtk themes via Home Manager === | ||
If you get [https://nix-community.github.io/home-manager/index.xhtml#_why_do_i_get_an_error_message_about_literal_ca_desrt_dconf_literal_or_literal_dconf_service_literal an error about {{ic|ca.desrt.conf}} or {{ic|dconf.service}}] on NixOS, add | If you get [https://nix-community.github.io/home-manager/index.xhtml#_why_do_i_get_an_error_message_about_literal_ca_desrt_dconf_literal_or_literal_dconf_service_literal an error about {{ic|ca.desrt.conf}} or {{ic|dconf.service}}] on NixOS, add | ||
| Line 181: | Line 187: | ||
=== Workaround with <code>home on tmpfs</code> and standalone installation === | === Workaround with <code>home on tmpfs</code> and standalone installation === | ||
home-on-tmpfs users who installed | home-on-tmpfs users who installed Home Manager standalone may meet problems that cannot load configs after reboot, caused by auto cleaning symlink under the toplevel of the home directory. You need to ensure <code>/home/<user>/.nix-profile</code> exists since the standalone install will not act symlink while the system boots. | ||
If your toplevel of home is on tmpfs, one possible workaround is manually write <code>activationScripts</code> to link the directory: | If your toplevel of home is on tmpfs, one possible workaround is manually write <code>activationScripts</code> to link the directory: | ||
| Line 200: | Line 206: | ||
== Templates == | == Templates == | ||
* https://github.com/juspay/nix-dev-home A | * https://github.com/juspay/nix-dev-home A Home Manager template providing useful tools & settings for Nix-based development. | ||
== Alternatives == | == Alternatives == | ||