Introduce a new overrideInputs on {default,shell}Nix

This allows the user of this non-flake to have similar functionality
to `follows`.

Example (default.nix):

    nix-repl> (import ./.).default
    «derivation /nix/store/axgvq22kyb8ymchzq1mjayms4jdl6ni6-ledc-0.1.0.drv»

    nix-repl> ((import ./.).overrideInputs { nixpkgs = <nixpkgs>; }).default
    «derivation /nix/store/r8zw693hpg91yx6f57hyx1gk4zdiq2lm-ledc-0.1.0.drv»
This commit is contained in:
mei (ckie) 2023-01-01 22:53:33 +02:00
parent 009399224d
commit 23b7a173da
Signed by: ckie
GPG key ID: 13E79449C0525215

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 "";
@ -186,15 +212,17 @@ let
then allNodes.${lockFile.root}
else throw "lock file '${lockFilePath}' has unsupported version ${toString lockFile.version}";
in
rec {
in
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 {}