From 56575cc0ac289d1f50358297cb72d9cd73f24630 Mon Sep 17 00:00:00 2001 From: Eric Sagnes Date: Fri, 29 Jul 2016 17:15:37 +0900 Subject: [PATCH 1/6] lib: add fileContents function --- lib/strings.nix | 10 ++++++++++ 1 file changed, 10 insertions(+) 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); } From 85d8b016600925791cbf970f69873dfac1f781cb Mon Sep 17 00:00:00 2001 From: Eric Sagnes Date: Sun, 31 Jul 2016 21:58:54 +0900 Subject: [PATCH 2/6] lib: refactor commitIdFromGitRepo with fileContents --- lib/sources.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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: From e276842f6adef45554f05f2aee8c938c8c651ed1 Mon Sep 17 00:00:00 2001 From: Eric Sagnes Date: Sun, 31 Jul 2016 21:59:30 +0900 Subject: [PATCH 3/6] lib: refactor nixpkgsVersion with fileContents --- lib/trivial.nix | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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"; From 1114ab41e6622cc4ac59ffb16e3bd291194e3208 Mon Sep 17 00:00:00 2001 From: Eric Sagnes Date: Sun, 31 Jul 2016 22:05:38 +0900 Subject: [PATCH 4/6] release.nix: refactor with fileContents --- nixos/release.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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}"; From c7bd26e5376da23e94c49515f0bff60964de4433 Mon Sep 17 00:00:00 2001 From: Eric Sagnes Date: Sun, 31 Jul 2016 22:08:29 +0900 Subject: [PATCH 5/6] version module: refactor with fileContents --- nixos/modules/misc/version.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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."; }; From 4c97e4fbffd5cba4fb1ae32172b730658972550a Mon Sep 17 00:00:00 2001 From: Eric Sagnes Date: Mon, 1 Aug 2016 18:34:54 +0900 Subject: [PATCH 6/6] make tarball: refactor with fileContents --- pkgs/top-level/make-tarball.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ];