Skip to content

nixos-rebuild-ng: validate NixOS configuration path#418243

Merged
thiagokokada merged 5 commits intoNixOS:masterfrom
thiagokokada:nixos-rebuild-validate-nixos-path
Jun 20, 2025
Merged

nixos-rebuild-ng: validate NixOS configuration path#418243
thiagokokada merged 5 commits intoNixOS:masterfrom
thiagokokada:nixos-rebuild-validate-nixos-path

Conversation

@thiagokokada
Copy link
Copy Markdown
Contributor

@thiagokokada thiagokokada commented Jun 19, 2025

When path:// or git+file:// protocol is used in Flake mode (that is the most common case since we normalize the paths, see PR #375493) and the current working directory in a symlink pointing base store path to the Nix store (e.g., /run/opengl-driver/lib), there is a nasty bug where Nix resolves the path as the Nix store path of the current derivation instead of the target derivation.

Since we blindly activate this path, this can corrupt the installation and break some other activation scripts, like systemd-boot-builder.py. While it is possible to recover this situation using nix-env -p /nix/var/nix/profiles/system --delete-generations old, this is far from ideal.

This commit solves it by validating that the resolved NixOS configuration path includes at least $out/nixos-version. I am not sure if this is going to break some cases so there is a escape hatch in the form of the environment variable
NIXOS_REBUILD_I_UNDERSTAND_THE_CONSEQUENCES_PLEASE_BREAK_MY_SYSTEM, but in general it looks safe.

Fix: #144811.

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • Nixpkgs 25.11 Release Notes (or backporting 24.11 and 25.05 Nixpkgs Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
  • NixOS 25.11 Release Notes (or backporting 24.11 and 25.05 NixOS Release notes)
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md, pkgs/README.md, maintainers/README.md and other contributing documentation in corresponding paths.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS labels Jun 19, 2025
@thiagokokada
Copy link
Copy Markdown
Contributor Author

@tejing1 I changed my opinion about validating the NixOS path. It is probably the only idea that will cover all cases (in #417191, you could still corrupt your system if you passed nixos-rebuild switch --flake path:/etc/nixos for example).

I will only validate the 2 destructive cases though to not complicate too much the code, that is either the switch or boot. In the other cases you will probably still get a crypt error message (e.g., boot-vm), but at least your system will not be damaged.

@tejing1
Copy link
Copy Markdown
Contributor

tejing1 commented Jun 19, 2025

Well, you already know I agree about going with validation. :-)

Implementation looks fine, though I didn't go over it in great detail. Hopefully the escape hatch is unnecessary, but better to have it and not need it than to need it and not have it...

@github-actions github-actions bot added the 12.approvals: 1 This PR was reviewed and approved by one person. label Jun 19, 2025
When `path://` or `git+file://` protocol is used in Flake mode (that is
the most common case since we normalize the paths, see PR NixOS#375493) and
the current working directory in a symlink pointing base store path to
the Nix store (e.g., /run/opengl-driver/lib), there is a nasty bug where
Nix resolves the path as the Nix store path of the current derivation
instead of the target derivation.

Since we blindly activate this path, this can corrupt the installation
and break some other activation scripts, like `systemd-boot-builder.py`.
While it is possible to recover this situation using `nix-env -p
/nix/var/nix/profiles/system --delete-generations old`, this is far from
ideal.

This commit solves it by validating that the resolved NixOS
configuration path includes at least `$out/nixos-version`. I am not sure
if this is going to break some cases so there is a escape hatch in the
form of the environment variable
`NIXOS_REBUILD_I_UNDERSTAND_THE_CONSEQUENCES_PLEASE_BREAK_MY_SYSTEM`,
but in general it looks safe.
@thiagokokada thiagokokada force-pushed the nixos-rebuild-validate-nixos-path branch from fdd55df to e364976 Compare June 19, 2025 22:51
@thiagokokada

This comment was marked as outdated.

@thiagokokada
Copy link
Copy Markdown
Contributor Author

@ofborg build nixos-rebuild-ng nixos-rebuild-ng.passthru.tests

@thiagokokada thiagokokada merged commit ae48ab3 into NixOS:master Jun 20, 2025
26 of 30 checks passed
@thiagokokada thiagokokada deleted the nixos-rebuild-validate-nixos-path branch June 20, 2025 14:11
thiagokokada added a commit to thiagokokada/nixpkgs that referenced this pull request Jun 22, 2025
In NixOS#418243 we started to validate NixOS config path to avoid a nasty bug
in Nix, but this doesn't work in the `--build-host` and `--target-host`
case because the configuration will not be available in the local host
to check if it contains a `nixos-version`. This moves the check to
`--target-host` instead, that is probably the correct choice anyway.

Fix NixOS#418868.
@nixpkgs-ci
Copy link
Copy Markdown
Contributor

nixpkgs-ci bot commented Jun 22, 2025

Successfully created backport PR for release-25.05:

@github-actions github-actions bot added the 8.has: port to stable This PR already has a backport to the stable release. label Jun 22, 2025
nixpkgs-ci bot pushed a commit that referenced this pull request Jun 22, 2025
In #418243 we started to validate NixOS config path to avoid a nasty bug
in Nix, but this doesn't work in the `--build-host` and `--target-host`
case because the configuration will not be available in the local host
to check if it contains a `nixos-version`. This moves the check to
`--target-host` instead, that is probably the correct choice anyway.

Fix #418868.

(cherry picked from commit 0f6624e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: port to stable This PR already has a backport to the stable release. 10.rebuild-darwin: 1-10 This PR causes between 1 and 10 packages to rebuild on Darwin. 10.rebuild-darwin: 1 This PR causes 1 package to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. 12.approvals: 1 This PR was reviewed and approved by one person.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nixos-rebuild is doing something bizarre if I run it from /run/opengl-driver/lib

3 participants