From 46773a15b3af302c3fb2a6e31fb589553a903099 Mon Sep 17 00:00:00 2001 From: Franz Pletz Date: Mon, 20 Jan 2020 00:44:07 +0100 Subject: [PATCH] nixos/version: fix case where .git is a symlink Before c9214c394b248e1f26e45dbe1be2bd82363af3a6 and 9d396d2e426b9a765de0b8999aa214f1259633e6 if .git is symlink the version would gracefully default to no git revision. With those changes an exception is thrown instead. This introduces a new function `pathIsGitRepo` that checks if `commitIdFromGitRepo` fails without error so we don't have to reimplement this logic again and can fail gracefully. --- lib/default.nix | 2 +- lib/sources.nix | 1 + lib/trivial.nix | 2 +- nixos/modules/misc/version.nix | 5 +++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/default.nix b/lib/default.nix index 5abafe1b2ac..d2e9f0e8086 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -101,7 +101,7 @@ let inherit (sources) pathType pathIsDirectory cleanSourceFilter cleanSource sourceByRegex sourceFilesBySuffices commitIdFromGitRepo cleanSourceWith pathHasContext - canCleanSource pathIsRegularFile; + canCleanSource pathIsRegularFile pathIsGitRepo; inherit (modules) evalModules unifyModuleSyntax applyIfFunction mergeModules mergeModules' mergeOptionDecls evalOptionValue mergeDefinitions diff --git a/lib/sources.nix b/lib/sources.nix index a5765c0fda5..05519c3e392 100644 --- a/lib/sources.nix +++ b/lib/sources.nix @@ -105,6 +105,7 @@ rec { in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts; in cleanSourceWith { inherit filter; src = path; }; + pathIsGitRepo = path: (builtins.tryEval (commitIdFromGitRepo path)).success; # Get the commit id of a git repo # Example: commitIdFromGitRepo diff --git a/lib/trivial.nix b/lib/trivial.nix index 940ec1a3d59..a281cd70fb0 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -191,7 +191,7 @@ rec { let revisionFile = "${toString ./..}/.git-revision"; gitRepo = "${toString ./..}/.git"; - in if builtins.pathExists gitRepo + in if lib.pathIsGitRepo gitRepo then lib.commitIdFromGitRepo gitRepo else if lib.pathExists revisionFile then lib.fileContents revisionFile else default; diff --git a/nixos/modules/misc/version.nix b/nixos/modules/misc/version.nix index ddbd3963cc5..8a85035ceb7 100644 --- a/nixos/modules/misc/version.nix +++ b/nixos/modules/misc/version.nix @@ -6,6 +6,7 @@ let cfg = config.system.nixos; gitRepo = "${toString pkgs.path}/.git"; + gitRepoValid = lib.pathIsGitRepo gitRepo; gitCommitId = lib.substring 0 7 (commitIdFromGitRepo gitRepo); in @@ -91,8 +92,8 @@ in # These defaults are set here rather than up there so that # changing them would not rebuild the manual version = mkDefault (cfg.release + cfg.versionSuffix); - revision = mkIf (pathExists gitRepo) (mkDefault gitCommitId); - versionSuffix = mkIf (pathExists gitRepo) (mkDefault (".git." + gitCommitId)); + revision = mkIf gitRepoValid (mkDefault gitCommitId); + versionSuffix = mkIf gitRepoValid (mkDefault (".git." + gitCommitId)); }; # Generate /etc/os-release. See