I should probably split this into separate pages. There’s Nix the language, Nix the package manager, nixpkgs the list of packages, and NixOS the operating system. There’s also nix flakes, which I haven’t used too much yet.
Nix allows you to do cool things like:
- Try programs without really “installing” them
- Have multiple versions of a program installed in parallel
- Atomically roll back a system
- Easily (uh, ish) override dependencies
- Build programs in a deterministic way, and even build them on more powerful computers and ship them back to you
I got into Nix because I would read John Wiegley’s personal .emacs configuration. He used to use git submodules to manage his complex Emacs dependencies, and then one day I noticed he switched to this thing called Nix. I first used Nix to also manage Emacs stuff (on Debian), then I eventually switched to NixOS.
Links
- The homepage of Nix & NixOS is nixos.org
- nixpkgs is hosted at github.com/NixOS/nixpkgs/
-
Search for Nix packages
-
Search for NixOS options
- NixOS Wiki (nixos.wiki) has some documentation that may help you troubleshoot stuff
- An Emacs Nix overlay is hosted at github.com/nix-community/emacs-overlay. This tracks the main branch of Emacs.
- Home Manager lets you configure packages and dotfiles with Nix
- Eelco Dolstra’s 2006 PhD thesis describing Nix is available at https://edolstra.github.io/pubs/phd-thesis.pdf.
Command Line stuff
This section might already be out of date, since some arguments have changed.
Start a Nix REPL with specific channel
nix repl -I nixpkgs=channel:nixos-unstable "<nixpkgs>"
These arguments may change later.
You can also use specific channels like nixos-21.11
NixOS configuration REPL
nix repl "<nixpkgs/nixos>"
You can look at configured variables under the config
set.
For example: config.networking.hostName
will say your configured hostname.
Build and install package from channel
Here’s one way to install the latest yt-dlp
:
nix build -f channel:nixos-unstable yt-dlp
This just builds (or usually just fetches) the derivation and stores the result in “./result
” in the current directory.
You can then install it with:
nix-env -i ./result
This is pretty imperative, and some would argue that a more proper thing to do would be to manage this in either your NixOS config or Home Manager. I won’t argue that. Sometimes upgrading all packages breaks them in weird ways. This might be fixed by Flakes.
Garbage collect old NixOS system profiles
As root (e.g. using sudo
) run:
nix-env --list-generations --profile /nix/var/nix/profiles/system
To get a list of NixOS system generations. Then run:
nix-env --delete-generations +1 --profile /nix/var/nix/profiles/system --dry-run
to delete everything except your current generation.
Of course, you’d do this without the --dry-run
argument.
You can also supply specific generation numbers instead of +1
.
To-Dos
- I’ve got some pretty cool ways of doing reproducible R and Stan analyses with Nix, which I also use to build analyses remotely. I should document some of that here.
- Should document how to make your own simple shell script packages.
- Fonts configuration can be hard to wrap your head around. Should document the environment variables for that and how to regenerate a font config cache.
Errors
Some documentation for some errors I’ve encountered, as I’ve encountered them.
Bus error while rebuilding NixOS
Encountered on 2023-12-07
Trying to finally upgrade to 23.11. I’m getting the following error while building:
building '/nix/store/1724qmj8zhyxibg1xqmrcnnj45anyipb-xwayland-23.2.2.drv'... unpacking sources unpacking source archive /nix/store/n5s7zab7lp7y162lwakk3k6qvpzis6sf-xwayland-23.2.2.tar.xz source root is xwayland-23.2.2 setting SOURCE_DATE_EPOCH to timestamp 1698198775 of file xwayland-23.2.2/xserver.ent.in patching sources updateAutotoolsGnuConfigScriptsPhase configuring mesonConfigurePhase flags: --prefix=/nix/store/hg13f7gy7671hs6mwz5r39swlsav5iay-xwayland-23.2.2 --libdir=/nix/store/hg13f7gy7671hs6mwz5r39swlsav5iay-xwayland-23.2.2/lib --libexecdir=/nix/store/hg13f7gy7671hs6mwz5r39swlsav5iay-xwayland-23.2.2/libexec --bindir=/nix/store/hg13f7gy7671hs6mwz5r39swlsav5iay-xwayland-23.2.2/bin --sbindir=/nix/store/hg13f7gy7671hs6mwz5r39swlsav5iay-xwayland-23.2.2/sbin --includedir=/nix/store/hg13f7gy7671hs6mwz5r39swlsav5iay-xwayland-23.2.2/include --mandir=/nix/store/hg13f7gy7671hs6mwz5r39swlsav5iay-xwayland-23.2.2/share/man --infodir=/nix/store/hg13f7gy7671hs6mwz5r39swlsav5iay-xwayland-23.2.2/share/info --localedir=/nix/store/hg13f7gy7671hs6mwz5r39swlsav5iay-xwayland-23.2.2/share/locale -Dauto_features=enabled -Dwrap_mode=nodownload --buildtype=plain -Dxwayland_eglstream=true -Dxcsecurity=true -Ddefault_font_path=/run/current-system/sw/share/X11/fonts -Dxkb_bin_dir=/nix/store/jlzjj0yn56pzgs5cwd69a335gb213sds-xkbcomp-1.4.6/bin -Dxkb_dir=/nix/store/w4m1s9q2hlisl2bbhxmlwmci1m7nxzdr-xkeyboard-config-2.40/etc/X11/xkb -Dxkb_output_dir=/nix/store/hg13f7gy7671hs6mwz5r39swlsav5iay-xwayland-23.2.2/share/X11/xkb/compiled -Dlibunwind=true /nix/store/qrmvf49dbs7yqshymnqdhaiig0mcljlz-meson-1.2.3/nix-support/setup-hook: line 3: 25 Bus error (core dumped) meson setup build "${flagsArray[@]}" error: builder for '/nix/store/1724qmj8zhyxibg1xqmrcnnj45anyipb-xwayland-23.2.2.drv' failed with exit code 135 error: 1 dependencies of derivation '/nix/store/b6bl6r130285xkwvwvjl17a9zain90dv-man-paths.drv' failed to build error: 1 dependencies of derivation '/nix/store/b4dm5ika5kya4z9g2mh60986i33vz9ff-system-path.drv' failed to build error: 1 dependencies of derivation '/nix/store/aaj3m75x9czpxgblzb8cw1755jvgwcks-X-Restart-Triggers-polkit.drv' failed to build error: 1 dependencies of derivation '/nix/store/r9y9ci2i9fw22qg6amvp8q9f78294vhz-dbus-1.drv' failed to build error: 1 dependencies of derivation '/nix/store/09bjggsj8fpnym3rfvf9g0kmjyxsckq9-etc-pam-environment.drv' failed to build error: 1 dependencies of derivation '/nix/store/z5lgypqfb8yi2xf4sknyjgkkkzi6v0kr-man-cache.drv' failed to build error: 1 dependencies of derivation '/nix/store/4kdnw0lq6agqjkbmv577g80nx18gh987-set-environment.drv' failed to build error: 1 dependencies of derivation '/nix/store/9b62fw5yq875d2ilvb7hjy9q5g6ydm6s-unit-accounts-daemon.service.drv' failed to build error: 1 dependencies of derivation '/nix/store/d0y49fm0s1migr0ccmff94ikif4mqpch-X-Restart-Triggers-dbus.drv' failed to build error: 1 dependencies of derivation '/nix/store/clijrdlji2y8y672da9n96jnqlzlh5ym-etc-man_db.conf.drv' failed to build error: 1 dependencies of derivation '/nix/store/6c7nkxscwi59ydq8i4r3sz7y4d0w8d59-etc-profile.drv' failed to build error: 1 dependencies of derivation '/nix/store/qyq637v81j9n2yvf3yypyffmcrima4jb-etc-zshenv.drv' failed to build error: 1 dependencies of derivation '/nix/store/2wyxrkdklscmzk21x4klfn2z289mklq6-unit-polkit.service.drv' failed to build error: 1 dependencies of derivation '/nix/store/gas3wlyzjd96lcz1gqz4hfdcljfggq2r-unit-dbus.service.drv' failed to build error: 1 dependencies of derivation '/nix/store/kjk081lvhz6hwrjbn8jm87lsl1aqxdmb-unit-dbus.service.drv' failed to build error: 3 dependencies of derivation '/nix/store/x1pzsi502467fn6wfvqdpbrv0vrq59z7-system-units.drv' failed to build error: 1 dependencies of derivation '/nix/store/mryj6fqplbb2yrcr6krkph3ajq9phpyx-user-units.drv' failed to build error: 9 dependencies of derivation '/nix/store/lk2d4dh23zrly4mdq7vwhpb48vs6wm75-etc.drv' failed to build
I know that I may have corrupted some nix store paths when I accidentally turned off my computer mid-upgrade. So I’ll look for corrupted paths and try to repair them:
nix-store --verify-path $(nix-store -qR /nix/store/1724qmj8zhyxibg1xqmrcnnj45anyipb-xwayland-23.2.2.drv)
This returned nothing. Not sure if that’s good or bad.
Looking at journalctl
I see:
Dec 07 21:00:45 ░░░░░ audit[59815]: ANOM_ABEND auid=1000 uid=30001 gid=30000 ses=3 subj=kernel pid=59815 comm="meson" exe="/nix/store/qp5zys77biz7imbk6yy85q5pdv7qk84j-python3-3.11.6/bin/python3.11" sig=7 res=1 Dec 07 21:00:45 ░░░░░ kernel: audit: type=1701 audit(1702011645.866:175): auid=1000 uid=30001 gid=30000 ses=3 subj=kernel pid=59815 comm="meson" exe="/nix/store/qp5zys77biz7imbk6yy85q5pdv7qk84j-python3-3.11.6/bin/python3.11" sig=7 res=1 Dec 07 21:00:45 ░░░░░ audit: BPF prog-id=338 op=LOAD Dec 07 21:00:45 ░░░░░ kernel: audit: type=1334 audit(1702011645.883:176): prog-id=338 op=LOAD Dec 07 21:00:45 ░░░░░ audit: BPF prog-id=339 op=LOAD Dec 07 21:00:45 ░░░░░ audit: BPF prog-id=334 op=UNLOAD
I’m going to try to build it while keeping the failed output:
NIX_PATH=nixpkgs=/nix/var/nix/profiles/per-user/root/channels/nixos:nixos-config=/░░░░░/configuration.nix:/nix/var/nix/profiles/per-user/root/channels nix build -vL --file "<nixpkgs/nixos>" system --keep-failed
Hmm… now that I think about it, why is it trying to build xwayland instead of fetch it from a cache? Did I override something a while back?
I’ll check to see why my system depends on it:
nix why-depends --derivation /nix/store/░░░░░-nixos-system-░░░░░-23.11.1431.░░░░░.drv /nix/store/1724qmj8zhyxibg1xqmrcnnj45anyipb-xwayland-23.2.2.drv
/nix/store/░░░░░-nixos-system-░░░░░-23.11.1431.░░░░░.drv └───/nix/store/b4dm5ika5kya4z9g2mh60986i33vz9ff-system-path.drv └───/nix/store/1724qmj8zhyxibg1xqmrcnnj45anyipb-xwayland-23.2.2.drv
Welp, that’s not really helpful.
Maybe I’ll compare its derivation to the standard xwayland
derivation, since they seem to be different.
Comparing a diff of
/nix/store/llklx9i8w71yib3x6338lq8ldhdnbqnl-xwayland-23.2.2.drv
(normal version)/nix/store/1724qmj8zhyxibg1xqmrcnnj45anyipb-xwayland-23.2.2.drv
(my weird version)
I see there’s a difference in mesonFlags
:
Normal version:
"mesonFlags": "-Dxwayland_eglstream=true -Dxcsecurity=true -Ddefault_font_path= -Dxkb_bin_dir=/nix/store/jlzjj0yn56pzgs5cwd69a335gb213sds-xkbcomp-1.4.6/bin -Dxkb_dir=/nix/store/w4m1s9q2hlisl2bbhxmlwmci1m7nxzdr-xkeyboard-config-2.40/etc/X11/xkb -Dxkb_output_dir=/1rz4g4znpzjwh1xymhjpm42vipw92pr73vdgl6xs1hycac8kf2n9/share/X11/xkb/compiled -Dlibunwind=true",
My version:
"mesonFlags": "-Dxwayland_eglstream=true -Dxcsecurity=true -Ddefault_font_path=/run/current-system/sw/share/X11/fonts -Dxkb_bin_dir=/nix/store/jlzjj0yn56pzgs5cwd69a335gb213sds-xkbcomp-1.4.6/bin -Dxkb_dir=/nix/store/w4m1s9q2hlisl2bbhxmlwmci1m7nxzdr-xkeyboard-config-2.40/etc/X11/xkb -Dxkb_output_dir=/1rz4g4znpzjwh1xymhjpm42vipw92pr73vdgl6xs1hycac8kf2n9/share/X11/xkb/compiled -Dlibunwind=true",
Why is mine depending on /run/current-system/sw/share/X11/fonts
?
The sandbox won’t have access to that.
Checking out a nix repl
for <nixpkgs/nixos>
I see that config.programs.xwayland.package
is indeed different than pkgs.xwayland
.
It looks like config.programs.xwayland.defaultFontPath
is getting set somewhere.
Maybe this has to do with this warning I ignored earlier:
trace: warning: The option `fonts.fonts' defined in `/░░░░░/configuration.nix' has been renamed to `fonts.packages'.
Looking at my settings, I see that I have the following:
fonts = {
fontDir.enable = true;
# enableFontDir = true;
fonts = with pkgs; [
I comment that fontDir.enable = true;
line out.
Now I see that the package matches up with the default one.
And now it builds correctly! (Though I haven’t checked if it actually works ;P)