Broken default installation of nix-bin and nix-setup-systemd

Bug #2064563 reported by Prasanna Loganathar
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
nix (Debian)
New
Undecided
Unassigned
nix (Ubuntu)
New
Undecided
Unassigned

Bug Description

# Context

- nix-bin can be setup in 2 ways.
  - One is to let .nix-defer user local directory handle both channels and packages. This will get placed into `/home/<user>/.local/state/nix/`
  - Second, is to let it all be symlinked into `nix/var/nix/profiles/per-user/<user>`. (This is handled by systemd-tmpfiles under `nix-setup-systemd` pkg. But very likely default installations do not enable system-tmpfiles for user, it'll likely be enabled globally, but user needs to enable manually)

## Current setup

- nix-setup-systemd ends up setting symlinks for per-user into the user directory here:
  - /usr/share/user-tmpfiles.d/nix-daemon.conf
- nix-setup-systemd also ends up setting up the NIX_PATH to per-user dir above making the assumption that it was completed in this file:
  - `/usr/lib/environment.d/nix-daemon.conf`

Firstly, this will be borked config in default installations unless the user enabled the systemd tmpfiles per user with

```
systemctl --user --now enable systemd-tmpfiles-setup.service
```

These symlinks will never be created and it's not in any of the docs. But after enabling, this sets up for the profile, but not for channels. It should also include symlinks for channels. This results in a broken installation, as `nix-env` will work after adding user channel as described in the docs. However all attempts to do `nix-shell -p <pkg>` will error due to:

```
warning: Nix search path entry '/nix/var/nix/profiles/per-user/<user>/channels/nixpkgs' does not exist, ignoring
warning: Nix search path entry '/nix/var/nix/profiles/per-user/<user>/channels' does not exist, ignoring
error:
       … <borked>

         at «none»:0: (source not available)

       … while calling the 'import' builtin
```

This is because there is no such symlink and NIX_PATH is set to the non-existent path.

## Current contents of

```
  4 #Type Path Mode UID GID Age Argument
  3 d %h/.nix-defexpr
  2 L %h/.nix-defexpr/channels_root - - - - /nix/var/nix/profiles/per-user/root/channels
  1 d /nix/var/nix/profiles/per-user/%u 0755
  5 L %h/.nix-profile - - - - /nix/var/nix/profiles/per-user/%u/profile
  1 d /nix/var/nix/gcroots/per-user/%u 0755

```

## Solutions

### Solution 1

Simplest. Remove excess config.

- Remove the symlinks entirely. Move /usr/share/user-tmpfiles.d/nix-daemon.conf to docs to remove depending on systemd-tmp files.
- Remove `NIX_PATH` in /usr/lib/environment.d/nix-daemon.conf. When NIX_PATH is not set, nix handles the user profile automatically and works well. (unset NIX_PATH in the shell can workaround this at the moment neatly)

### Solution 2

More nix data inside /nix store

- Add channels to the symlink in /usr/share/user-tmpfiles.d/nix-daemon.conf.
- Auto-enable `systemctl --user enable systemd-tmpfiles-setup.service`?

ProblemType: Bug
DistroRelease: Ubuntu 24.04
Package: nix-setup-systemd 2.18.1+dfsg-1ubuntu5
ProcVersionSignature: Ubuntu 6.8.0-31.31-generic 6.8.1
Uname: Linux 6.8.0-31-generic x86_64
ApportVersion: 2.28.1-0ubuntu2
Architecture: amd64
CasperMD5CheckResult: pass
CurrentDesktop: ubuntu:GNOME
Date: Thu May 2 12:44:59 2024
InstallationDate: Installed on 2024-04-26 (6 days ago)
InstallationMedia: Ubuntu 24.04 LTS "Noble Numbat" - Release amd64 (20240424)
PackageArchitecture: all
SourcePackage: nix
UpgradeStatus: No upgrade log present (probably fresh install)

Revision history for this message
Prasanna Loganathar (pvl) wrote :
Revision history for this message
Prasanna Loganathar (pvl) wrote :

This is really odd. There is no package called nix. Only nix-bin and nix-setup-systemd. But launchpad will only let me tag nix. Wonder if there is another launchpad bug or I'm missing something.

description: updated
Revision history for this message
Peter Valdemar Mørch (pmorch) wrote :

@pvl: There is a _source_ package called `nix`. That generates the installable packages `nix-bin` and `nix-setup-systemd`.

Revision history for this message
Peter Valdemar Mørch (pmorch) wrote :

In addition, nix environment variables don't get set up when one logs in via ssh.

 I As the OP points out:

> nix-setup-systemd also ends up setting up the NIX_PATH to per-user dir above making the assumption that it was completed in this file:
> - `/usr/lib/environment.d/nix-daemon.conf`

It also sets up NIX_REMOTE and modifies PATH.

The problem is that /usr/lib/environment.d/nix-daemon.conf is sourced by systemd-environment-d-generator(8) that parses them and updates the environment exported by the systemd user instance (see https://www.freedesktop.org/software/systemd/man/latest/environment.d.html). Which means the environment variables are set for processes started by the systemd user instance (only).

And not when you login via ssh.

Because of this, nix works (except for the problems presented previously in this bug) only for the desktop session, and not when logged in via ssh.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.