Oops! - deleted all my default-profiles

Trying to understand garbage colletion I confused the system-*-link links with the default-*-link links.
As seen in one of the nix pills, I started to delete the “default-*-link” links - but unfortunately all of them!
In /nix/var/nix/proflies it now looks like:

[root@nixtn:~]$ ls -l /nix/var/nix/profiles/
insgesamt 32
lrwxrwxrwx 1 root root   14  9. Feb 23:59 default -> default-4-link
drwxrwxrwt 6 root root 4096 10. Feb 20:34 per-user
lrwxrwxrwx 1 root root   14 11. Feb 17:43 system -> system-25-link
lrwxrwxrwx 1 root root   89  4. Feb 21:32 system-19-link -> /nix/store/7hc0qcfbyrr3gl8v78npyzp03kx929hs-nixos-system-nixtn-19.03pre166987.bc41317e243
lrwxrwxrwx 1 root root   89  4. Feb 21:49 system-20-link -> /nix/store/l8dskwpf8f7ri8pdxi4iq6a507b5hn24-nixos-system-nixtn-19.03pre166987.bc41317e243
lrwxrwxrwx 1 root root   89  9. Feb 23:42 system-21-link -> /nix/store/p84h94f1b0bkgblh1az6kwq43yms1gkh-nixos-system-nixtn-19.03pre168613.ffc604e5579
lrwxrwxrwx 1 root root   89 10. Feb 00:19 system-22-link -> /nix/store/x2nv1qpxq03kp6svh5mryr9s5pgxlqb3-nixos-system-nixtn-19.03pre168613.ffc604e5579
lrwxrwxrwx 1 root root   89 10. Feb 07:17 system-23-link -> /nix/store/17vj66jjhkckb6bnldpkp5sbjkf5qr3q-nixos-system-nixtn-19.03pre168725.64825dfd26a
lrwxrwxrwx 1 root root   89 10. Feb 20:18 system-24-link -> /nix/store/rirb4zv1q8wj7y4an1ix03hifc498car-nixos-system-nixtn-19.03pre168725.64825dfd26a
lrwxrwxrwx 1 root root   89 11. Feb 17:43 system-25-link -> /nix/store/gf84jwn6lgjn5v3s3sq0w1xia043jr6g-nixos-system-nixtn-19.03pre168781.929cc78363e

As you can see, the default link points to default-4-link which I unfortunately removed…

Any ideas how to repair this? Running a nixos-rebuild switch didn’t restore the missed link.

1 Like

nixos-rebuild touches the system profile. The default profile is used by nix-env when run as the user root. Installing something as root via nix-env will update that profile.

1 Like

My system doesn’t even have /nix/var/nix/profiles/default at all which makes me believe that it’s not needed for normal operation (possibly because I never used nix-env as root).
@spacefrogg If nix-env as root populates /nix/var/nix/profiles/default then do you perhaps know the purpose of /nix/var/nix/profiles/per-user/root/?

Thanks a lot!
Where did you learn about this? I really read the docs as good as I could (Nix manual, Nix wiki, Nix pills), but didn’t find this info…

@matix You’re right, it is not needed. Every user can have his/her user-specific profiles in /nix/var/nix/profiles/per-user/<username>. This is a conventional location and shell initialization ensures on every login that the user owns this directory.
Then you have to keep in mind the distinction between nix the package manager and NixOS the operating system. NixOS also defines other locations for profiles, e.g. system and per-container/. nix the package manager understands a single-user (i.e. global user) mode and a multi-user mode of installation. In both modes there is an elevated user, who, by default uses the profile default for nix-env operations. It’s purpose is to make all changes to the nix-environment (hence, nix-env) visible to all users. If on NixOS you look into your PATH environment variable on the shell, you’ll see that every user sees the profile default as well as his/her own per-user/* profile.

@mrtn22 Look at the nix-env manpage for other clues. I think, it is not documented so prominently, because 1. standalone multi-user nix is not in widespread use, and 2. NixOS discourages you to use nix-env as root to exercise a global effect for all users, at last 3. in single-user nix, you don’t really care for the name of the profile.

@matix So, what is the purpose of /nix/var/nix/profiles/per-user/root/? It tracks the nix channels that root has installed. Users don’t normally see the channels that root uses, they just install and track their own.

Thanks for detailed explanation.

When I installed my system and created my user, Nix actually created a symlink ~/.nix-defexpr/channels_root -> /nix/var/nix/profiles/per-user/root/channels alongside the usual ~/.nix-defexpr/channels

Of course! That’s how it works.