From b67ee6e861903abb04e9024d605dfc7b00922633 Mon Sep 17 00:00:00 2001 From: Artturin Date: Sun, 6 Nov 2022 06:16:13 +0200 Subject: [PATCH 1/3] lib/trivial: fix 'error: cannot decode virtual path '/nix/store/virtual0000000000000000000000005-source'' happens on lazy-trees branch of Nix (NixOS/nix#6530) --- lib/trivial.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/trivial.nix b/lib/trivial.nix index 5d4fad8266bc..8f2023caaf8e 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -213,8 +213,8 @@ rec { # Default value to return if revision can not be determined default: let - revisionFile = "${toString ./..}/.git-revision"; - gitRepo = "${toString ./..}/.git"; + revisionFile = ./.. + "/.git-revision"; + gitRepo = ./.. + "/.git"; in if lib.pathIsGitRepo gitRepo then lib.commitIdFromGitRepo gitRepo else if lib.pathExists revisionFile then lib.fileContents revisionFile From 8c1b0192c5cf48e176bd4937787ba548e050395d Mon Sep 17 00:00:00 2001 From: Artturin Date: Sun, 6 Nov 2022 06:32:22 +0200 Subject: [PATCH 2/3] lib/sources: remove 2 usages of toString on a path which will be read using fileContents It gives a warning on the lazy-trees branch of Nix (NixOS/nix#6530) "warning: applying 'toString' to path '...' and then accessing it is deprecated, at '...'" 'else toString (/. + "${base}/${path}");' at line 183 may still cause a warning but i don't know how to reach that codepath and test so im leaving it untouched changing it to 'else /. + "${base}/${path}";' caused this error ``` error: a string that refers to a store path cannot be appended to a path at /home/systems/nixpkgs/lib/sources.nix:183:20: 182| then path 183| else /. + "${base}/${path}"; | ^ 184| in if pathIsRegularFile path ``` --- lib/sources.nix | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sources.nix b/lib/sources.nix index cec395c9bb18..51836696cf10 100644 --- a/lib/sources.nix +++ b/lib/sources.nix @@ -175,8 +175,8 @@ let */ commitIdFromGitRepo = let readCommitFromFile = file: path: - let fileName = toString path + "/" + file; - packedRefsName = toString path + "/packed-refs"; + let fileName = path + "/${file}"; + packedRefsName = path + "/packed-refs"; absolutePath = base: path: if lib.hasPrefix "/" path then path From ec8f8f69bd57b9dfd24ed56ef8f3c53c9ee5f453 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 7 Nov 2022 12:52:13 +0100 Subject: [PATCH 3/3] lib/sources: Make pathIsGitRepo not evaluate toString path This requires us to avoid the `tryEval` + `throw` combination, because throw is strict in its error message, and we don't want to drop our single clue when `commitIdFromGitRepo` is used incorrectly. --- lib/sources.nix | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/sources.nix b/lib/sources.nix index 51836696cf10..9db3d2329542 100644 --- a/lib/sources.nix +++ b/lib/sources.nix @@ -166,14 +166,25 @@ let in type == "directory" || lib.any (ext: lib.hasSuffix ext base) exts; in cleanSourceWith { inherit filter src; }; - pathIsGitRepo = path: (tryEval (commitIdFromGitRepo path)).success; + pathIsGitRepo = path: (commitIdFromGitRepoOrError path)?value; /* Get the commit id of a git repo. Example: commitIdFromGitRepo */ - commitIdFromGitRepo = + commitIdFromGitRepo = path: + let commitIdOrError = commitIdFromGitRepoOrError path; + in commitIdOrError.value or (throw commitIdOrError.error); + + /* + Get the commit id of a git repo. + + Returns `{ value = commitHash }` or `{ error = "... message ..." }`. + + Example: commitIdFromGitRepo + */ + commitIdFromGitRepoOrError = let readCommitFromFile = file: path: let fileName = path + "/${file}"; packedRefsName = path + "/packed-refs"; @@ -186,7 +197,7 @@ let then let m = match "^gitdir: (.*)$" (lib.fileContents path); in if m == null - then throw ("File contains no gitdir reference: " + path) + then { error = "File contains no gitdir reference: " + path; } else let gitDir = absolutePath (dirOf path) (lib.head m); commonDir'' = if pathIsRegularFile "${gitDir}/commondir" @@ -204,7 +215,7 @@ let let fileContent = lib.fileContents fileName; matchRef = match "^ref: (.*)$" fileContent; in if matchRef == null - then fileContent + then { value = fileContent; } else readCommitFromFile (lib.head matchRef) path else if pathIsRegularFile packedRefsName @@ -218,10 +229,10 @@ let # https://github.com/NixOS/nix/issues/2147#issuecomment-659868795 refs = filter isRef (split "\n" fileContent); in if refs == [] - then throw ("Could not find " + file + " in " + packedRefsName) - else lib.head (matchRef (lib.head refs)) + then { error = "Could not find " + file + " in " + packedRefsName; } + else { value = lib.head (matchRef (lib.head refs)); } - else throw ("Not a .git directory: " + path); + else { error = "Not a .git directory: " + toString path; }; in readCommitFromFile "HEAD"; pathHasContext = builtins.hasContext or (lib.hasPrefix storeDir);