diff options
| author | Emily <vcs@emily.moe> | 2023-07-07 09:02:38 +0100 |
|---|---|---|
| committer | Emily <vcs@emily.moe> | 2023-07-09 07:26:23 +0100 |
| commit | 51ba5e614acf1c94d519e6047814219082827faa (patch) | |
| tree | 4cf52aa43eaedc0874949b3d7c06179a21932961 /modules/system/version.nix | |
| parent | 72b7e8668c24950b8300ffe3d135dad1ae72a4f5 (diff) | |
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.
Diffstat (limited to 'modules/system/version.nix')
| -rw-r--r-- | modules/system/version.nix | 81 |
1 files changed, 43 insertions, 38 deletions
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"; + } ]; }; } |
