From 72b7e8668c24950b8300ffe3d135dad1ae72a4f5 Mon Sep 17 00:00:00 2001 From: Emily Date: Fri, 7 Jul 2023 09:02:38 +0100 Subject: version: default Git revision options to `null` This allows for more uniform handling in the documentation generator, and avoids lying about the Git reference being `master` internally. --- modules/system/version.nix | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'modules/system') diff --git a/modules/system/version.nix b/modules/system/version.nix index 6801918..0824829 100644 --- a/modules/system/version.nix +++ b/modules/system/version.nix @@ -67,8 +67,8 @@ in system.darwinRevision = mkOption { internal = true; - type = types.str; - default = "master"; + type = types.nullOr types.str; + default = null; description = lib.mdDoc "The darwin git revision from which this configuration was built."; }; @@ -92,7 +92,8 @@ in system.nixpkgsRevision = mkOption { internal = true; - type = types.str; + type = types.nullOr types.str; + default = null; description = lib.mdDoc "The nixpkgs git revision from which this configuration was built."; }; }; -- cgit v1.2.3 From 51ba5e614acf1c94d519e6047814219082827faa Mon Sep 17 00:00:00 2001 From: Emily Date: Fri, 7 Jul 2023 09:02:38 +0100 Subject: version: rewrite Git revision logic We trust the version information from `nixpkgs.source` when `pkgs` was constructed by the `nixpkgs` module or `nixpkgs.source` was explicitly set by the configuration. Otherwise, we rely on Nixpkgs to report its own version, which handles the same cases as the old logic and opens the door to Nixpkgs automatically reporting the correct revision when using flakes. --- modules/system/version.nix | 81 ++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 38 deletions(-) (limited to 'modules/system') diff --git a/modules/system/version.nix b/modules/system/version.nix index 0824829..e45f667 100644 --- a/modules/system/version.nix +++ b/modules/system/version.nix @@ -1,4 +1,4 @@ -{ options, config, lib, pkgs, ... }: +{ options, config, lib, ... }: with lib; @@ -7,27 +7,28 @@ let defaultStateVersion = options.system.stateVersion.default; - parseGit = path: - if pathExists "${path}/.git" then rec { - rev = commitIdFromGitRepo "${path}/.git"; - shortRev = substring 0 7 rev; - } - else if pathExists "${path}/.git-revision" then rec { - rev = fileContents "${path}/.git-revision"; - shortRev = substring 0 7 rev; - } - else { - shortRev = "0000000"; - }; - - darwin = parseGit (toString ../..); - nixpkgs = parseGit (toString pkgs.path); - - releaseFile = "${toString pkgs.path}/.version"; - suffixFile = "${toString pkgs.path}/.version-suffix"; - - nixpkgsSuffix = if pathExists suffixFile then fileContents suffixFile - else ".git." + nixpkgs.shortRev; + # Based on `lib.trivial.revisionWithDefault` from nixpkgs. + gitRevision = path: + if pathIsGitRepo "${path}/.git" + then commitIdFromGitRepo "${path}/.git" + else if pathExists "${path}/.git-revision" + then fileContents "${path}/.git-revision" + else null; + + nixpkgsSrc = config.nixpkgs.source; + + # If `nixpkgs.constructedByUs` is true, then Nixpkgs was imported from + # `nixpkgs.source` and we can use revision information (flake input, + # `builtins.fetchGit`, etc.) from it. Otherwise `pkgs` could be + # anything and we can't reliably determine exact version information, + # but if the configuration explicitly sets `nixpkgs.source` we + # trust it. + useSourceRevision = + (config.nixpkgs.constructedByUs + || options.nixpkgs.source.highestPrio < (lib.mkDefault {}).priority) + && isAttrs nixpkgsSrc + && (nixpkgsSrc._type or null == "flake" + || isString (nixpkgsSrc.rev or null)); in { @@ -56,63 +57,67 @@ in system.darwinVersion = mkOption { internal = true; type = types.str; - description = lib.mdDoc "The full darwin version (e.g. `darwin4.master`)."; + default = "darwin${toString cfg.stateVersion}${cfg.darwinVersionSuffix}"; + description = lib.mdDoc "The full darwin version (e.g. `darwin4.2abdb5a`)."; }; system.darwinVersionSuffix = mkOption { internal = true; type = types.str; + default = if cfg.darwinRevision != null + then ".${substring 0 7 cfg.darwinRevision}" + else ""; description = lib.mdDoc "The short darwin version suffix (e.g. `.2abdb5a`)."; }; system.darwinRevision = mkOption { internal = true; type = types.nullOr types.str; - default = null; + default = gitRevision (toString ../..); description = lib.mdDoc "The darwin git revision from which this configuration was built."; }; system.nixpkgsRelease = mkOption { readOnly = true; type = types.str; + default = lib.trivial.release; description = lib.mdDoc "The nixpkgs release (e.g. `16.03`)."; }; system.nixpkgsVersion = mkOption { internal = true; type = types.str; + default = cfg.nixpkgsRelease + cfg.nixpkgsVersionSuffix; description = lib.mdDoc "The full nixpkgs version (e.g. `16.03.1160.f2d4ee1`)."; }; system.nixpkgsVersionSuffix = mkOption { internal = true; type = types.str; + default = if useSourceRevision + then ".${lib.substring 0 8 (nixpkgsSrc.lastModifiedDate or nixpkgsSrc.lastModified or "19700101")}.${nixpkgsSrc.shortRev or "dirty"}" + else lib.trivial.versionSuffix; description = lib.mdDoc "The short nixpkgs version suffix (e.g. `.1160.f2d4ee1`)."; }; system.nixpkgsRevision = mkOption { internal = true; type = types.nullOr types.str; - default = null; + default = if useSourceRevision && nixpkgsSrc ? rev + then nixpkgsSrc.rev + else lib.trivial.revisionWithDefault null; description = lib.mdDoc "The nixpkgs git revision from which this configuration was built."; }; }; config = { - - # These defaults are set here rather than up there so that - # changing them would not rebuild the manual + # This default is set here rather than up there so that the options + # documentation is not reprocessed on every commit system.darwinLabel = mkDefault "${cfg.nixpkgsVersion}+${cfg.darwinVersion}"; - system.darwinVersion = mkDefault "darwin${toString cfg.stateVersion}${cfg.darwinVersionSuffix}"; - system.darwinVersionSuffix = mkDefault ".${darwin.shortRev}"; - system.darwinRevision = mkIf (darwin ? rev) (mkDefault darwin.rev); - - system.nixpkgsVersion = mkDefault "${cfg.nixpkgsRelease}${cfg.nixpkgsVersionSuffix}"; - system.nixpkgsRelease = mkDefault (fileContents releaseFile); - system.nixpkgsVersionSuffix = mkDefault nixpkgsSuffix; - system.nixpkgsRevision = mkIf (nixpkgs ? rev) (mkDefault nixpkgs.rev); - - assertions = [ { assertion = cfg.stateVersion <= defaultStateVersion; message = "system.stateVersion = ${toString cfg.stateVersion}; is not a valid value"; } ]; + assertions = [ { + assertion = cfg.stateVersion <= defaultStateVersion; + message = "system.stateVersion = ${toString cfg.stateVersion}; is not a valid value"; + } ]; }; } -- cgit v1.2.3 From f9724c4543035d6190c00168ebfa93f0b2e927d0 Mon Sep 17 00:00:00 2001 From: Emily Date: Fri, 7 Jul 2023 09:02:38 +0100 Subject: eval-config: rationalize handling of Nixpkgs This is a big change that disentangles a lot of mistaken assumptions about mixing multiple versions of Nixpkgs, treating external flake inputs as gospel for the source of Nixpkgs and nix-darwin, etc.; the end result should be much simpler conceptually, but it will be a breaking change for anyone using `eval-config.nix` directly. Hopefully that shouldn't be a big issue, as it is more of an internal API and it's quite likely that existing uses may have been broken in the same way the internal ones were. It was previously easy to get into a state where your `lib` comes from nix-darwin's `nixpkgs` input or a global channel and your `pkgs` comes from another major version of Nixpkgs. This is pretty fundamentally broken due to the coupling of `pkgs` to its corresponding `lib`, but the brokenness was hidden much of the time until something surfaced it. Now there is exactly one mandatory `lib` input to system evaluation, and the handling of various additional options like `pkgs` and `system` can be done modularly; maintaining backwards compatibility with the previous calling convention is punted to the `default.nix` and `lib.darwinSystem` entry points. `inputs` is no longer read by nix-darwin or special in any way, merely a convention for user code, and the argument is retained in the entry points only for backwards compatibility. All correct invocations of the entry points should keep working after this change, and some previously-broken ones should be fixed too. The documentation and template have been adjusted to show the newly-recommended modular way of specifying various things, but no deprecation warnings have been introduced yet by this change. There is one potential, mostly cosmetic regression: `system.nixpkgsRevision` and related options are less likely to be set than before, in cases where it is not possible to determine the origin of the package set. Setting `nixpkgs.source` explicitly will make this work again, and I hope to look into sending changes upstream to Nixpkgs to make `lib.trivial.revisionWithDefault` behave properly under flakes, which would fix this regression and potentially allow reducing some of the complexity. Fixes: #669 --- modules/system/flake-overrides.nix | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 modules/system/flake-overrides.nix (limited to 'modules/system') diff --git a/modules/system/flake-overrides.nix b/modules/system/flake-overrides.nix deleted file mode 100644 index df3eb16..0000000 --- a/modules/system/flake-overrides.nix +++ /dev/null @@ -1,20 +0,0 @@ -{ lib, inputs, ... }: - -with lib; - -let - inherit (inputs) darwin nixpkgs; -in - -{ - config = { - system.checks.verifyNixPath = mkDefault false; - system.checks.verifyNixChannels = mkDefault false; - - system.darwinVersionSuffix = ".${darwin.shortRev or "dirty"}"; - system.darwinRevision = mkIf (darwin ? rev) darwin.rev; - - system.nixpkgsVersionSuffix = ".${substring 0 8 (nixpkgs.lastModifiedDate or nixpkgs.lastModified or "19700101")}.${nixpkgs.shortRev or "dirty"}"; - system.nixpkgsRevision = mkIf (nixpkgs ? rev) nixpkgs.rev; - }; -} -- cgit v1.2.3