Introduce a new overrideInputs on {default,shell}Nix #49

Closed
ckiee wants to merge 2 commits from ckiee/add-overrideInputs into master
2 changed files with 208 additions and 179 deletions

View file

@ -18,7 +18,8 @@ Afterwards, create a `default.nix` file containing the following:
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
url = with lock.nodes.flake-compat.locked;
"https://github.com/${owner}/${repo}/archive/${rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)

View file

@ -8,7 +8,11 @@
{ src, system ? builtins.currentSystem or "unknown-system" }:
let
id = x: x;
overrideWrap' = fn: set: set // { overrideInputs = ov: fn (makeFlakeCompat ov); };
overrideWrap = overrideWrap' id;
makeFlakeCompat = impureOverrides:
let
lockFilePath = src + "/flake.lock";
lockFile = builtins.fromJSON (builtins.readFile lockFilePath);
@ -104,6 +108,16 @@ let
{ lastModified = 0; lastModifiedDate = formatSecondsSinceEpoch 0; }
// (if src ? outPath then src else tryFetchGit src);
nameValuePair = name: value:
{ inherit name value; };
mapAttrs' =
# A function, given an attribute's name and value, returns a new `nameValuePair`.
f:
# Attribute set to map over.
set:
builtins.listToAttrs (map (attr: f attr set.${attr}) (builtins.attrNames set));
# Format number of seconds in the Unix epoch as %Y%m%d%H%M%S.
formatSecondsSinceEpoch = t:
let
@ -129,6 +143,11 @@ let
pad = s: if builtins.stringLength s < 2 then "0" + s else s;
in "${toString y'}${pad (toString m)}${pad (toString d)}${pad (toString hours)}${pad (toString minutes)}${pad (toString seconds)}";
rootOverrides =
mapAttrs'
(input: lockKey: nameValuePair lockKey (impureOverrides.${input} or null))
lockFile.nodes.${lockFile.root}.inputs;
allNodes =
builtins.mapAttrs
(key: node:
@ -136,7 +155,14 @@ let
sourceInfo =
if key == lockFile.root
then rootSrc
else fetchTree (node.info or {} // removeAttrs node.locked ["dir"]);
else
if rootOverrides.${key} != null then
{ type = "path";
outPath = rootOverrides.${key};
narHash = throw "narHash unimplemented for impureOverride";
}
else
fetchTree (node.info or {} // removeAttrs node.locked ["dir"]);
subdir = if key == lockFile.root then "" else node.locked.dir or "";
@ -187,14 +213,16 @@ let
else throw "lock file '${lockFilePath}' has unsupported version ${toString lockFile.version}";
in
rec {
builtins.mapAttrs (key: attr: overrideWrap' (res: res.${key}) attr)
(rec {
defaultNix =
(builtins.removeAttrs result ["__functor"])
// (if result ? defaultPackage.${system} then { default = result.defaultPackage.${system}; } else {})
// (if result ? packages.${system}.default then { default = result.packages.${system}.default; } else {});
shellNix =
defaultNix
// (if result ? devShell.${system} then { default = result.devShell.${system}; } else {})
// (if result ? devShells.${system}.default then { default = result.devShells.${system}.default; } else {});
}
});
in makeFlakeCompat {}