Assuming if you have completed all of the Getting Started instructions below, you should be able to run the following command to apply the dotfiles / installed package changes.
home-manager switch --impure --flake .#$HOMECONFIG_NAME
# OR
nix build .#homeManagerConfigurations.$HOMECONFIG_NAME.activationPackage && ./result/activate
# OR After applying at least once, using the alias
hmapply $HOMECONFIG_NAMETo update the packages for home-manager, simply run the nix-channel update and flake-lock updates first, then run the home-manager switch command above.
nix-channel --update
nix flake update ~/.config/nixpkgs
# OR
nxup
To check the updated list of $HOMECONFIG_NAME, run the following command and use the name immediately after the homeConfigurations. prefix
$ awk '/^ ###### HOMECONFIG PROFILES START/{p=1;next};/^ ###### HOMECONFIG PROFILES END/{p=0};p' ~/.config/nixpkgs/flake.nix | awk -F'=' '{print $1}' | awk '{$1=$1;print}'
linux_64
linux_headless_64
darwin_64
# OR After applying at least once, using the alias
hmlsThe above advocates for --impure purely because we are using our $HOME and $USER env vars to
switch the home-manager config. We could probably remove --impure if we set the username and homeDirectory for the respective homeConfigurations available, or just define a new homeConfiguration that does not use those.
When applying the home-manager switch, occasionally Nix will fail to install the new home manager profile. Due to the a file conflict between home-manager-path and nix. An example of such a scenario here: nix-community/home-manager#2995
A workaround is to set the priority of the corresponding nix package to be 0, taking higher priority
than home-manager-path
nix-env --set-flag priority 0 nix-2.10.3
Relevant Link: https://nix-community.github.io/home-manager/index.html#sec-usage-rollbacks
Home Manager as of time of writing does not support rollbacks explicitly but the following steps can be used to help with this:
# 1. Run the following commands to see the generation you'll want to rollback to
$ home-manager generations
2022-07-31 11:44 : id 3 -> /nix/store/c4nxmvixgan6iw3h4qyb35mrllip4rzc-home-manager-generation
2022-07-30 23:54 : id 2 -> /nix/store/6ybk9xq9i44v9afy27wwjpk2wz92z0cr-home-manager-generation
2022-07-30 13:52 : id 1 -> /nix/store/qh89xz60pdiwizzr7q2mijm7iv8mh7dg-home-manager-generation
# 2. Copy the store path of the generation you'll like to rollback to and run its `activate` script
# For example if we want to rollback to generation 1
$ /nix/store/qh89xz60pdiwizzr7q2mijm7iv8mh7dg-home-manager-generation/activate
Starting home manager activationCleaning up unused nix stores can be done via the following:
# First cleanup home-generations, can replace `now` with relative days such as '-30days` etc
home-manager expire-generations now
# OR the folowing after applying once
hmclean
nix-store --gc
# Or the following after applying once
nxclean
# The following to erase every generation for nix-store
nix-collect-garbage -d
# Or the following after applying once
nxcleandeepTaken from: https://nixos.wiki/wiki/Nix#Imperative_Operations
nix environments also technically allow imperative operations, although the home-manager switch .#$HOMECONFIG_NAME should overwrite this
Below are some nix-env common commands used to run imperatively
nix search packagename # Searching for packages
nix-env -iA packagename # Installing a package, for example in `nix-shell`, you can run `nix-env -iA nixpkgs.custom_python310_with_defaults`
nix-env -q # List installed packages
nix-env -e packagename # Uninstall packages
nix-env -u # Upgrade packages
nix-env --list-generations # see nix-env generations
nix-env --delete-generations +3 # remove every generation except current + 2 older
nix-env --delete-generations old # remove all old generations
nix-channel --list # list all installed channels
nix-channel --update # update all nix channels
nix-channel --remove channelname # remove channel
nix-channel --add CHANNEL_URL channel_name # add a new channel to track, e.g. nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs-unstableSometimes you'll want to try out temporary builds and not overwrite the current system, in this case
nix allows us to drop into a temporary shell environment via nix-shell.
To drop into this shell, with the project's overlays applied, do the following:
# cd into the nixpkgs path (i.e. ~/.config/nixpkgs), the path for this repo
cd ~/.config/nixpkgs
# drop into the environment, this should take a while if its your first time as
# the store will take a while to fetch / build
# the packages required
nix-shell --pureInstall nix, there several recommendation that NixOS has, you can check here: https://nixos.org/download.html
Here is a general gist of the installation process:
Nix Installations comes in different flavours, multi-user installation and single-user installation.
# Multi-user installation
sh <(curl -L https://nixos.org/nix/install) --daemon
# Single user installation
sh <(curl -L https://nixos.org/nix/install) --no-daemonThe choice of installations determine how nix will be run. Under a multi-user installation, the nix store is owned by root and builds are done via special user accounts. Unprivileged users forward nix-commands as actions to the nix-daemon.
Multi-user installations have a few pros and cons over a single-user installation.
- Better build isolation (and that is what Nix is all about)
- Better security (a build can not write somewhere in your home)
- Sharing builds between users
- Requires
rootto run the daemon - More involved installation (creation of
nixbld* users, installing asystemdunit, ... - Harder to uninstall
- multi-user installation is recommended if you are on Linux running systemd, with SELinux disabled and you can authenticate with sudo.
- single-user is recommended otherwise.
/nixwill be owned by the invoking user. You should run this under your usual user account, not asroot. The script will invokesudoto create/nixif it doesn’t already exist.
- single-user installation is recommended
- multi-user installation is recommended
$ nix-env --version
nix-env (Nix) 2.3.15
nix-channel --list
# Add nixpkgs unstable if not added automatically
nix-channel --add https://nixos.org/channels/nixpkgs-unstable nixpkgs
nix-channel --updateNix Flakes allows us to specify our dependencies in a declarative way
Ensure that nix >= 2.4, then add this line in the config path ~/.config/nix/nix.conf
experimental-features = nix-command flakes
Restart the nix-daemon
# On linux
sudo systemctl restart nix-daemon.service
# On MacOS
sudo launchctl stop org.nixos.nix-daemon
sudo launchctl start org.nixos.nix-daemonMore recent installations of nix should already come with flakes installed. To verify, check with the following command:
nix flake --helpAdd the following Home Manager channel.
nix-channel --add https://github.com/nix-community/home-manager/archive/master.tar.gz home-manager
nix-channel --updateRun the Home Manager installation command and create the first Home Manager generation:
NIX_PATH=$HOME/.nix-defexpr/channels:/nix/var/nix/profiles/per-user/root/channels${NIX_PATH:+:$NIX_PATH} nix-shell '<home-manager>' -A installClone the repository to the following path ~/.config/nixpkgs.
Overwrite the git name and git email for this as well in this step
git clone git@github.com:lohvht/dotfiles.git ~/.config/nixpkgs
git config --add user.name "Victor Loh"
git config --add user.email vic94loh@hotmail.comAfter that, proceed with the home-manager switch instruction at the start of the document
This repository consists of packages and dotfiles installations that can only be done at the $HOME level, as this is not a NixOS module.
While certain tools and packages (such as some language support for Python/Golang/Rust etc) can be installed here, others tools may require installation at a system level, such as via either your native package manager or manual installation.
Below in the following section are some of the starting points for some tools that I use
Install docker desktop here: https://docs.docker.com/get-docker/
- Add the full path of the desired shell to the end of the
/etc/shellsfile
The .envrc
export FOO=foo > .envrc
e.g.
echo '/home/myunixusername/.nix-profile/bin/zsh' >> /etc/shells
echo '/home/myunixusername/.nix-profile/bin/bash' >> /etc/shells- Change shell with the following
chsh -s ~/.nix-profile/bin/fishhttps://nixos.org/manual/nix/stable/installation/upgrading.html
For updating nix itself, these are the commands to run: Multi-user Nix users on macOS can upgrade Nix by running:
sudo -i sh -c 'nix-channel --update && nix-env -iA nixpkgs.nix && launchctl remove org.nixos.nix-daemon && launchctl load /Library/LaunchDaemons/org.nixos.nix-daemon.plist'
Single-user installations of Nix should run this:
nix-channel --update; nix-env -iA nixpkgs.nix nixpkgs.cacert
Multi-user Nix users on Linux should run this with sudo:
nix-channel --update; nix-env -iA nixpkgs.nix nixpkgs.cacert; systemctl daemon-reload; systemctl restart nix-daemon
nix flake update ~/.config/nixpkgs
# After first apply
nxup