Merge pull request #17357 from ericsagnes/feat/fileContents

lib: add fileContents function

authored by zimbatm.tngl.sh and committed by GitHub f8108c12 6dd27481

+22 -10
+3 -3
lib/sources.nix
··· 44 44 packedRefsName = toString path + "/packed-refs"; 45 45 in if lib.pathExists fileName 46 46 then 47 - let fileContent = readFile fileName; 47 + let fileContent = lib.fileContents fileName; 48 48 # Sometimes git stores the commitId directly in the file but 49 49 # sometimes it stores something like: «ref: refs/heads/branch-name» 50 - matchRef = match "^ref: (.*)\n$" fileContent; 50 + matchRef = match "^ref: (.*)$" fileContent; 51 51 in if isNull matchRef 52 - then lib.removeSuffix "\n" fileContent 52 + then fileContent 53 53 else readCommitFromFile path (lib.head matchRef) 54 54 # Sometimes, the file isn't there at all and has been packed away in the 55 55 # packed-refs file, so we have to grep through it:
+10
lib/strings.nix
··· 479 479 absolutePaths = builtins.map (path: builtins.toPath (root + "/" + path)) relativePaths; 480 480 in 481 481 absolutePaths; 482 + 483 + /* Read the contents of a file removing the trailing \n 484 + 485 + Example: 486 + $ echo "1.0" > ./version 487 + 488 + fileContents ./version 489 + => "1.0" 490 + */ 491 + fileContents = file: removeSuffix "\n" (builtins.readFile file); 482 492 }
+4 -2
lib/trivial.nix
··· 62 62 isInt add sub lessThan 63 63 seq deepSeq genericClosure; 64 64 65 + inherit (import ./strings.nix) fileContents; 66 + 65 67 # Return the Nixpkgs version number. 66 68 nixpkgsVersion = 67 69 let suffixFile = ../.version-suffix; in 68 - readFile ../.version 69 - + (if pathExists suffixFile then readFile suffixFile else "pre-git"); 70 + fileContents ../.version 71 + + (if pathExists suffixFile then fileContents suffixFile else "pre-git"); 70 72 71 73 # Whether we're being called by nix-shell. 72 74 inNixShell = builtins.getEnv "IN_NIX_SHELL" == "1";
+3 -3
nixos/modules/misc/version.nix
··· 49 49 nixosRelease = mkOption { 50 50 readOnly = true; 51 51 type = types.str; 52 - default = readFile releaseFile; 52 + default = fileContents releaseFile; 53 53 description = "The NixOS release (e.g. <literal>16.03</literal>)."; 54 54 }; 55 55 56 56 nixosVersionSuffix = mkOption { 57 57 internal = true; 58 58 type = types.str; 59 - default = if pathExists suffixFile then readFile suffixFile else "pre-git"; 59 + default = if pathExists suffixFile then fileContents suffixFile else "pre-git"; 60 60 description = "The NixOS version suffix (e.g. <literal>1160.f2d4ee1</literal>)."; 61 61 }; 62 62 63 63 nixosRevision = mkOption { 64 64 internal = true; 65 65 type = types.str; 66 - default = if pathExists revisionFile then readFile revisionFile else "master"; 66 + default = if pathExists revisionFile then fileContents revisionFile else "master"; 67 67 description = "The Git revision from which this NixOS configuration was built."; 68 68 }; 69 69
+1 -1
nixos/release.nix
··· 7 7 8 8 let 9 9 10 - version = builtins.readFile ../.version; 10 + version = fileContents ../.version; 11 11 versionSuffix = 12 12 (if stableBranch then "." else "pre") + "${toString nixpkgs.revCount}.${nixpkgs.shortRev}"; 13 13
+1 -1
pkgs/top-level/make-tarball.nix
··· 15 15 src = nixpkgs; 16 16 17 17 inherit officialRelease; 18 - version = builtins.readFile ../../.version; 18 + version = pkgs.lib.fileContents ../../.version; 19 19 versionSuffix = "pre${toString nixpkgs.revCount}.${nixpkgs.shortRev}"; 20 20 21 21 buildInputs = [ nix.out jq ];