From 5be632dab0be072deadd69ebced5bc7366d71bb8 Mon Sep 17 00:00:00 2001 From: Esteve Fernandez Date: Mon, 4 May 2026 22:17:46 +0200 Subject: [PATCH] gpu: use tmpfiles.d to set up the /run/opengl-driver symlink Signed-off-by: Esteve Fernandez --- docs/manual/usage/gpu-non-nixos.md | 11 +++++---- .../misc/news/2026/05/2026-05-04_22-44-35.nix | 13 +++++++++++ .../generic-linux/gpu/setup/default.nix | 6 ++--- .../gpu/setup/non-nixos-gpu-setup | 23 ++++++++++++------- .../gpu/setup/non-nixos-gpu.conf | 1 + .../gpu/setup/non-nixos-gpu.service | 10 -------- .../generic-linux-gpu/nvidia-enabled.nix | 8 +++---- .../setup-package-contents.nix | 2 +- 8 files changed, 43 insertions(+), 31 deletions(-) create mode 100644 modules/misc/news/2026/05/2026-05-04_22-44-35.nix create mode 100644 modules/targets/generic-linux/gpu/setup/non-nixos-gpu.conf delete mode 100644 modules/targets/generic-linux/gpu/setup/non-nixos-gpu.service diff --git a/docs/manual/usage/gpu-non-nixos.md b/docs/manual/usage/gpu-non-nixos.md index 937e89561..0adabd25d 100644 --- a/docs/manual/usage/gpu-non-nixos.md +++ b/docs/manual/usage/gpu-non-nixos.md @@ -38,16 +38,17 @@ GPU drivers require an update, run Because the `/run` directory is volatile and disappears on reboot, libraries cannot be simply copied or linked there. The `non-nixos-gpu-setup` script -installs a Systemd service which ensures that the drivers are linked to -`/run/opengl-driver` on boot. Home Manager will always check and warn you when -this setup needs to be refreshed. +installs a +[`tmpfiles.d`](https://www.freedesktop.org/software/systemd/man/latest/tmpfiles.d.html) +config which ensures that the drivers are linked to `/run/opengl-driver` on +boot. Home Manager will always check and warn you when this setup needs to be +refreshed. If you ever wish to uninstall these drivers, all you need to do is ```sh sudo rm /run/opengl-driver -sudo systemctl disable --now non-nixos-gpu.service -sudo rm /etc/systemd/system/non-nixos-gpu.service +sudo rm /etc/tmpfiles.d/non-nixos-gpu.conf ``` diff --git a/modules/misc/news/2026/05/2026-05-04_22-44-35.nix b/modules/misc/news/2026/05/2026-05-04_22-44-35.nix new file mode 100644 index 000000000..861d2cb03 --- /dev/null +++ b/modules/misc/news/2026/05/2026-05-04_22-44-35.nix @@ -0,0 +1,13 @@ +{ config, ... }: +{ + time = "2026-05-04T20:44:35+00:00"; + condition = config.targets.genericLinux.gpu.enable; + message = '' + The GPU driver setup for non-NixOS systems has been switched from + a systemd service to a tmpfiles.d configuration. + + If you have previously run 'non-nixos-gpu-setup', you will need + to run it again to migrate. The script will automatically clean + up the old systemd service and install the new tmpfiles.d config. + ''; +} diff --git a/modules/targets/generic-linux/gpu/setup/default.nix b/modules/targets/generic-linux/gpu/setup/default.nix index 4922faaac..b0de92389 100644 --- a/modules/targets/generic-linux/gpu/setup/default.nix +++ b/modules/targets/generic-linux/gpu/setup/default.nix @@ -20,12 +20,12 @@ stdenv.mkDerivation { substituteInPlace non-nixos-gpu* \ --replace-quiet '@@resources@@' "$out/resources" \ --replace-quiet '@@statedir@@' '${nixStateDirectory}' \ - --replace-quiet '@@systemddir@@' "$out/lib/systemd/system" \ + --replace-quiet '@@tmpfilesdir@@' "$out/lib/tmpfiles.d" \ --replace-quiet '@@env@@' "${nonNixosGpuEnv}" ''; installPhase = '' - mkdir -p $out/{bin,resources,lib/systemd/system} + mkdir -p $out/{bin,resources,lib/tmpfiles.d} cp non-nixos-gpu-setup $out/bin - cp non-nixos-gpu.service $out/lib/systemd/system + cp non-nixos-gpu.conf $out/lib/tmpfiles.d ''; } diff --git a/modules/targets/generic-linux/gpu/setup/non-nixos-gpu-setup b/modules/targets/generic-linux/gpu/setup/non-nixos-gpu-setup index 2d4051678..c257d180f 100755 --- a/modules/targets/generic-linux/gpu/setup/non-nixos-gpu-setup +++ b/modules/targets/generic-linux/gpu/setup/non-nixos-gpu-setup @@ -2,12 +2,19 @@ set -e -# Install the systemd service file and ensure that the store path won't be -# garbage-collected as long as it's installed. -unit_path=/etc/systemd/system/non-nixos-gpu.service -ln -sf @@systemddir@@/non-nixos-gpu.service "$unit_path" -ln -sf "$unit_path" "@@statedir@@"/gcroots/non-nixos-gpu.service +# Migrate from old systemd service setup +old_unit=/etc/systemd/system/non-nixos-gpu.service +if [[ -L "$old_unit" || -f "$old_unit" ]]; then + systemctl disable --now non-nixos-gpu.service 2>/dev/null || true + rm -f "$old_unit" + rm -f "@@statedir@@/gcroots/non-nixos-gpu.service" + systemctl daemon-reload +fi -systemctl daemon-reload -systemctl enable non-nixos-gpu.service -systemctl restart non-nixos-gpu.service +# Install the tmpfiles.d config file and ensure that the store path won't be +# garbage-collected as long as it's installed. +conf_path=/etc/tmpfiles.d/non-nixos-gpu.conf +ln -sf @@tmpfilesdir@@/non-nixos-gpu.conf "$conf_path" +ln -sf "$conf_path" "@@statedir@@"/gcroots/non-nixos-gpu.conf + +systemd-tmpfiles --create non-nixos-gpu.conf diff --git a/modules/targets/generic-linux/gpu/setup/non-nixos-gpu.conf b/modules/targets/generic-linux/gpu/setup/non-nixos-gpu.conf new file mode 100644 index 000000000..7f982139c --- /dev/null +++ b/modules/targets/generic-linux/gpu/setup/non-nixos-gpu.conf @@ -0,0 +1 @@ +L+ /run/opengl-driver - - - - @@env@@ diff --git a/modules/targets/generic-linux/gpu/setup/non-nixos-gpu.service b/modules/targets/generic-linux/gpu/setup/non-nixos-gpu.service deleted file mode 100644 index 9ce6054a8..000000000 --- a/modules/targets/generic-linux/gpu/setup/non-nixos-gpu.service +++ /dev/null @@ -1,10 +0,0 @@ -[Unit] -Description=GPU driver setup for Nix on non-NixOS Linux systems - -[Install] -WantedBy=multi-user.target - -[Service] -Type=oneshot -ExecStart=ln -nsf @@env@@ /run/opengl-driver -RemainAfterExit=yes diff --git a/tests/modules/targets-linux/generic-linux-gpu/nvidia-enabled.nix b/tests/modules/targets-linux/generic-linux-gpu/nvidia-enabled.nix index c497675dc..d60194b13 100644 --- a/tests/modules/targets-linux/generic-linux-gpu/nvidia-enabled.nix +++ b/tests/modules/targets-linux/generic-linux-gpu/nvidia-enabled.nix @@ -39,15 +39,15 @@ in setupScript="$TESTED/home-path/bin/non-nixos-gpu-setup" assertFileExists "$setupScript" - # Find the service file + # Find the tmpfiles.d config file storePath="$(dirname "$(readlink "''${setupScript}")")"/../ - servicePath="$storePath/lib/systemd/system/non-nixos-gpu.service" + confPath="$storePath/lib/tmpfiles.d/non-nixos-gpu.conf" # Extract the GPU environment path - envPath=$(grep -oP '/nix/store/[^/]+-non-nixos-gpu' "$servicePath" | head -1) + envPath=$(grep -oP '/nix/store/[^/]+-non-nixos-gpu' "$confPath" | head -1) if [[ -z "$envPath" ]]; then - fail "Could not find GPU environment path in service file" + fail "Could not find GPU environment path in config file" fi markerFile="$envPath/lib/nvidia-test-marker" diff --git a/tests/modules/targets-linux/generic-linux-gpu/setup-package-contents.nix b/tests/modules/targets-linux/generic-linux-gpu/setup-package-contents.nix index b2274cbc1..807e3696c 100644 --- a/tests/modules/targets-linux/generic-linux-gpu/setup-package-contents.nix +++ b/tests/modules/targets-linux/generic-linux-gpu/setup-package-contents.nix @@ -19,7 +19,7 @@ # Check that expected files are present and free of placeholders storePath="$(dirname "$(readlink "''${setupScript}")")"/../ expectedFiles=( - lib/systemd/system/non-nixos-gpu.service + lib/tmpfiles.d/non-nixos-gpu.conf ) for f in "''${expectedFiles[@]}"; do