nixos-rebuild-ng: disable flake auto-detection when --file or --attr is used (#527046)

This commit is contained in:
Thiago Kenji Okada
2026-06-05 17:48:35 +00:00
committed by GitHub
3 changed files with 24 additions and 1 deletions

View File

@@ -314,13 +314,16 @@ It must be one of the following:
be made using _nixos_ function in nixpkgs or importing and calling
nixos/lib/eval-config.nix from nixpkgs. If specified without *--attr*
option, builds the configuration from the top-level attribute set of the
file.
file. Using this option disables automatic flake detection, same as
*--no-flake*.
*--attr* _attrPath_, *-A* _attrPath_
Build the NixOS system from a nix file and use the specified
attribute path from the file specified by the *--file* option.
If specified without *--file* option, uses _system.nix_ in current directory,
the system-wide _<nixos-system>_ file, or finally, /etc/nixos/system.nix.
Using this option disables automatic flake detection, same as
*--no-flake*.
*--flake* _flake-uri[#name]_, *-F* _flake-uri[#name]_
Build the NixOS system from the specified flake. It defaults to the

View File

@@ -329,6 +329,10 @@ def parse_args(
if args.flake and (args.file or args.attr):
parser.error("--flake cannot be used with --file or --attr")
if (args.file or args.attr) and args.flake is None:
# Disable flake auto-detection when --file or --attr is used
args.flake = False
if args.store_path:
if args.rollback:
parser.error("--store-path and --rollback are mutually exclusive")

View File

@@ -89,6 +89,22 @@ def test_parse_args() -> None:
assert r_store_path.flake is False
assert r_store_path.store_path == "/nix/store/foo"
# --file and --attr should disable flake auto-detection
r_file, _ = nr.parse_args(["nixos-rebuild", "switch", "--file", "foo.nix"])
assert r_file.flake is False
assert r_file.file == "foo.nix"
r_attr, _ = nr.parse_args(["nixos-rebuild", "switch", "--attr", "bar"])
assert r_attr.flake is False
assert r_attr.attr == "bar"
r_file_attr, _ = nr.parse_args(
["nixos-rebuild", "switch", "--file", "foo.nix", "--attr", "bar"]
)
assert r_file_attr.flake is False
assert r_file_attr.file == "foo.nix"
assert r_file_attr.attr == "bar"
r1, g1 = nr.parse_args(
[
"nixos-rebuild",