diff --git a/Makefile b/Makefile index 439bfe04..50acd4e2 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ SHARE ?= $(PREFIX)/share/nixos-generator all: -SOURCES = formats format-module.nix configuration.nix nixos-generate.nix +SOURCES = formats format-module.nix configuration.nix lib.nix nixos-generate.nix install: mkdir -p $(PREFIX)/bin $(SHARE) diff --git a/formats/kexec.nix b/formats/kexec.nix index a124454d..47878fbb 100644 --- a/formats/kexec.nix +++ b/formats/kexec.nix @@ -1,9 +1,11 @@ -{ config, pkgs, lib, modulesPath, ... }: let +{ config, pkgs, lib, modulesPath, options, ... }: let clever-tests = builtins.fetchGit { url = "https://github.com/cleverca22/nix-tests"; rev = "a9a316ad89bfd791df4953c1a8b4e8ed77995a18"; # master on 2021-06-13 }; + + inherit (import ../lib.nix { inherit lib options; }) maybe; in { imports = [ "${toString modulesPath}/installer/netboot/netboot-minimal.nix" @@ -13,7 +15,7 @@ in { ]; system.build = rec { - kexec_tarball = lib.mkForce (pkgs.callPackage "${toString modulesPath}/../lib/make-system-tarball.nix" { + kexec_tarball = maybe.mkForce (pkgs.callPackage "${toString modulesPath}/../lib/make-system-tarball.nix" { storeContents = [ { object = config.system.build.kexec_script; symlink = "/kexec_nixos"; } ]; diff --git a/formats/raw-efi.nix b/formats/raw-efi.nix index e566e0af..6ebabad7 100644 --- a/formats/raw-efi.nix +++ b/formats/raw-efi.nix @@ -1,5 +1,8 @@ -{ config, lib, pkgs, modulesPath, ... }: -{ +{ config, lib, options, pkgs, modulesPath, ... }: + +let + inherit (import ../lib.nix { inherit lib options; }) maybe; +in { imports = [ ./raw.nix ]; boot.loader.grub = { @@ -13,7 +16,7 @@ fsType = "vfat"; }; - system.build.raw = lib.mkOverride 99 (import "${toString modulesPath}/../lib/make-disk-image.nix" { + system.build.raw = maybe.mkOverride 99 (import "${toString modulesPath}/../lib/make-disk-image.nix" { inherit lib config pkgs; partitionTableType = "efi"; diskSize = "auto"; diff --git a/lib.nix b/lib.nix new file mode 100644 index 00000000..83e365e0 --- /dev/null +++ b/lib.nix @@ -0,0 +1,32 @@ +{ + lib, + options, +}: let + # See https://github.com/NixOS/nixpkgs/commit/ccb85a53b6a496984073227fd8c4d4c58889f421 + # This commit changed the type of `system.build` from a lazy attribute set to + # a submodule. Prior to this commit, it doesn't make sense to qualify, e.g. + # the `system.build.kexec_tarball` definition with `lib.mkForce`, as this + # would result in setting the (final/resolved) value of + # `system.build.kexec_tarball` to something like: + # { + # _type = "override"; + # content = <...>; + # priority = 50; + # } + # However, since this commit, `system.build.kexec_tarball` *must* be defined + # using `lib.mkForce`; otherwise, Nix bails out with a complaint about + # `system.build.kexec_tarball` being defined in multiple locations. + systemBuildIsSubmodule = options.system.build.type.name == "submodule"; + + optionsLookSane = lib.hasAttrByPath ["system" "build" "type" "name"] options; +in + assert (lib.assertMsg optionsLookSane "`options' must be the NixOS module `options' argument"); { + maybe = + { + mkForce = lib.id; + mkOverride = _: lib.id; + } + // (lib.optionalAttrs systemBuildIsSubmodule { + inherit (lib) mkForce mkOverride; + }); + }