Skip to content

bryango/cheznix

Repository files navigation

cheznix

nix home-manager setup, documented below.

bootstrap

  • install nix, via either pacman or the determinate installer
  • prefer --daemon, namely the "multi-user" install
  • set up nix.conf and restart nix-daemon.service
  • confirm that ~/.nix-profile & env is correctly set up
  • note that $PATH is usually taken care of by the installer through /etc/profile/nix{,-daemon}.sh
  • clone and apply the profile:
dest="$HOME/.config/home-manager"

nix flake clone github:bryango/cheznix --dest "$dest" && cd "$dest" || exit
nix run . -- switch --update-input nixpkgs-config --show-trace
## ^ home-manager provided as flake `packages.${system}.default`
  • set up additional env in ~/.profile
  • upon successful activation, manage with the {home,system}-manager commands in place of the nix run commands

nix: more pacman beyond pacman

Why?

  • arch & AUR is perfect.
  • unfortunately, Real Life is messy.

Multiple versions are sometimes a requirement, which pacman refuses to handle. Fortunately, we have nix!

  • read on for some quick tips
  • jump to #intro for some basics

strategies

For package management:

  • use nix at the user level, as much as possible
  • fall back to pacman, AUR or whatever for system / graphical / incompatible packages

Some limitations of nix packages in non-NixOS:

  • gui apps are often faulty: lack of graphics, theming, audio, input method...
  • apps that rely on system bin / lib may have troubles

For home (dot)files management,

  • do not over manage, leave that to chezmoi
  • ... unless they need to be configured, in which case one can make use of modules
  • in particular, migrate non-secret config from ~/.secrets to home.nix

cli quick start

nix search nixpkgs neovim

## check output store path & size
nix eval --raw nixpkgs#neovim.outPath \
| xargs nix path-info --store https://cache.nixos.org \
   -hs  ## human readable size
        ## -r: recurse closures, -S: closure size

## dirty install
nix profile install nixpkgs#neovim
  ## --profile "~/.local/state/nix/profiles/$profile"

debugging

nix profile diff-closures
  ## --profile "~/.local/state/nix/profiles/$profile"

## derivations
nix-diff ~/.local/state/nix/profiles/$profile-{$old,$new}-link

nix why-depends \
  --derivation \
  --all \
  --precise
## sanitize the outputs: `sed -E 's|/nix/store/.{32}-| |g'`

nix-store --query

garbage collection

See https://nixos.org/manual/nix/unstable/package-management/garbage-collection.html.

# check access points (roots)
nix-store --gc --print-roots

# actual garbage collection
nix-store -v --gc

# further optimisation
nix-store --optimise

More AGRESSIVE:

# delete old generations
nix profile wipe-history --older-than "$num"d
home-manager expire-generations '2023-07-04 08:00'

# all-in-one util
nix-collect-garbage  # --delete-older-than, --max-freed, --dry-run

To get an overview of package sizes,

nix path-info --all -hs | sort -hk2
nix path-info --json --all \
  | jq 'map(.narSize) | add' \
  | numfmt --to=iec-i --format=%.2f
  ## total size of the store

nix intro

  • profile: virtual environments, managed with nix profile
  • registry: index of packages (flakes), managed with nix registry
  • channels: deprecated, special profiles which contain snapshots of the nixpkgs repo

See: https://nixos.org/manual/nix/unstable/package-management/profiles.html

$ ls -alF --time-style=+ --directory ~/.nix* | sed -E "s/$USER/\$USER/g" 
.nix-channels  ## deprecated, removed
.nix-defexpr/
.nix-profile -> .local/state/nix/profiles/profile/

registry

This is the package index for nix, analogous to that of a traditional package manager such as pacman, but made reproducible through version pinning. This is just like a modern build system such as cargo.

nix registry list

## refresh index & pin (to latest / to hash)
nix registry pin nixpkgs
nix registry add nixpkgs github:NixOS/nixpkgs/dc6263a3028cb06a178c16a0dd11e271752e537b

One can also alias / override / add local repositories; this is done automatically in modules/flake-channels.nix.

Although nixpkgs is mostly rolling, mass rebuild of packages are bundled together through the staging workflow. To get a list of staging-next merges, go to:

See also doc/staging-bisect.md.

binary cache substituters

Here we follow the guidance of tuna.

Note: either trusted-users or trusted-substituters has to be declared in the root config /etc/nix/nix.conf. Otherwise substituters will be ignored. This is not emphasized, neither in the manual nor the error message. See NixOS/nix#6672.

convenient channel

Note: channel is deprecated but we can set up a convenient compatible layer with the flake registry; see the relevant settings in modules/flake-channels.nix.

To add a channel temporarily, one can specify:

  • $NIX_PATH, or
  • --include nixpkgs=channel:$channel, or
  • -I nixpkgs=flake:$channel

such that nixpkgs is easily available via import <nixpkgs> {}. The list of channels are found in:

profiles

$ ls -alF --time-style=+ ~/.local/state/nix/profiles | sed -E "s/$USER/\$USER/g"          
profile -> profile-$gen-link/
profile-$gen-link -> /nix/store/#some-hash
  • $gen in $profile-$gen-link is the generation number.
  • profile is the default user profile
  • /nix/var/nix/profiles/default -> /nix/var/nix/profiles/per-user/root/profile is the default profile

The files are well-documented in man nix-env and man nix3-profile, except for the last one: /nix/var/nix/profiles/default which seems to be undocumented but useful.

History: the profile locations have been changed before! See NixOS/nix#5226. The new defaults seem more reasonable.

flakes

nixConfig trusted settings are stored in ~/.local/share/nix/trusted-settings.json.

About

nix home-manager setup

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published