diff --git a/lib/sources.nix b/lib/sources.nix index 535b04523b57..c1ec02b9c26c 100644 --- a/lib/sources.nix +++ b/lib/sources.nix @@ -44,12 +44,12 @@ rec { packedRefsName = toString path + "/packed-refs"; in if lib.pathExists fileName then - let fileContent = readFile fileName; + let fileContent = lib.fileContents fileName; # Sometimes git stores the commitId directly in the file but # sometimes it stores something like: «ref: refs/heads/branch-name» - matchRef = match "^ref: (.*)\n$" fileContent; + matchRef = match "^ref: (.*)$" fileContent; in if isNull matchRef - then lib.removeSuffix "\n" fileContent + then fileContent else readCommitFromFile path (lib.head matchRef) # Sometimes, the file isn't there at all and has been packed away in the # packed-refs file, so we have to grep through it: diff --git a/lib/strings.nix b/lib/strings.nix index 5e5f7b378667..daf845839343 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -479,4 +479,14 @@ rec { absolutePaths = builtins.map (path: builtins.toPath (root + "/" + path)) relativePaths; in absolutePaths; + + /* Read the contents of a file removing the trailing \n + + Example: + $ echo "1.0" > ./version + + fileContents ./version + => "1.0" + */ + fileContents = file: removeSuffix "\n" (builtins.readFile file); } diff --git a/lib/trivial.nix b/lib/trivial.nix index 9821e3c138dd..f85c74ab88e3 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -62,11 +62,13 @@ rec { isInt add sub lessThan seq deepSeq genericClosure; + inherit (import ./strings.nix) fileContents; + # Return the Nixpkgs version number. nixpkgsVersion = let suffixFile = ../.version-suffix; in - readFile ../.version - + (if pathExists suffixFile then readFile suffixFile else "pre-git"); + fileContents ../.version + + (if pathExists suffixFile then fileContents suffixFile else "pre-git"); # Whether we're being called by nix-shell. inNixShell = builtins.getEnv "IN_NIX_SHELL" == "1"; diff --git a/nixos/modules/misc/version.nix b/nixos/modules/misc/version.nix index 6d4d3a9eea34..6af310a9d877 100644 --- a/nixos/modules/misc/version.nix +++ b/nixos/modules/misc/version.nix @@ -49,21 +49,21 @@ in nixosRelease = mkOption { readOnly = true; type = types.str; - default = readFile releaseFile; + default = fileContents releaseFile; description = "The NixOS release (e.g. 16.03)."; }; nixosVersionSuffix = mkOption { internal = true; type = types.str; - default = if pathExists suffixFile then readFile suffixFile else "pre-git"; + default = if pathExists suffixFile then fileContents suffixFile else "pre-git"; description = "The NixOS version suffix (e.g. 1160.f2d4ee1)."; }; nixosRevision = mkOption { internal = true; type = types.str; - default = if pathExists revisionFile then readFile revisionFile else "master"; + default = if pathExists revisionFile then fileContents revisionFile else "master"; description = "The Git revision from which this NixOS configuration was built."; }; diff --git a/nixos/release.nix b/nixos/release.nix index 9886fdea291c..4647a02afb1c 100644 --- a/nixos/release.nix +++ b/nixos/release.nix @@ -7,7 +7,7 @@ with import ../lib; let - version = builtins.readFile ../.version; + version = fileContents ../.version; versionSuffix = (if stableBranch then "." else "pre") + "${toString nixpkgs.revCount}.${nixpkgs.shortRev}"; diff --git a/pkgs/top-level/make-tarball.nix b/pkgs/top-level/make-tarball.nix index b664dceaa954..2fe69390ec5b 100644 --- a/pkgs/top-level/make-tarball.nix +++ b/pkgs/top-level/make-tarball.nix @@ -15,7 +15,7 @@ releaseTools.sourceTarball rec { src = nixpkgs; inherit officialRelease; - version = builtins.readFile ../../.version; + version = pkgs.lib.fileContents ../../.version; versionSuffix = "pre${toString nixpkgs.revCount}.${nixpkgs.shortRev}"; buildInputs = [ nix.out jq ];