Compare commits

..

2 commits

Author SHA1 Message Date
mei (ckie) 98797efd23
Change README.md instructions to be slightly more versatile.
Accept other forks also with type="github".

This is enough to be a bit more convenient in a few more
cases once this propagates into new future projects.
2023-01-01 23:47:37 +02:00
mei (ckie) 23b7a173da
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»
2023-01-01 23:42:00 +02:00
3 changed files with 211 additions and 240 deletions

View file

@ -2,25 +2,13 @@
## Usage
> [!WARNING]
> During the Lix private beta period during which Forgejo is private, this
> requires configuring netrc in Lix for the tarball download to work.
>
> Your netrc should look something like so:
> ```
> machine git.lix.systems login YOUR-USERNAME password SOME-PERSONAL-ACCESS-TOKEN-REPO-READ
> ```
>
> We are terribly sorry for the UX for this being very bad
> ([issue](https://git.lix.systems/lix-project/lix/issues/254)).
To use, add the following to your `flake.nix`:
<!-- FIXME: this can use the standard non-api archive url when we are
un-privated -->
```nix
inputs.flake-compat.url = "https://git.lix.systems/api/v1/repos/lix-project/flake-compat/archive/main.tar.gz";
inputs.flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
```
Afterwards, create a `default.nix` file containing the following:
@ -30,7 +18,8 @@ Afterwards, create a `default.nix` file containing the following:
(
let lock = builtins.fromJSON (builtins.readFile ./flake.lock); in
fetchTarball {
url = lock.nodes.flake-compat.locked.url;
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);
@ -63,17 +67,8 @@ let
);
shortRev = builtins.substring 0 7 info.rev;
}
else if info.type == "sourcehut" then
{ inherit (info) rev narHash lastModified;
outPath =
fetchTarball
({ url = "https://${info.host or "git.sr.ht"}/${info.owner}/${info.repo}/archive/${info.rev}.tar.gz"; }
// (if info ? narHash then { sha256 = info.narHash; } else {})
);
shortRev = builtins.substring 0 7 info.rev;
}
else
# FIXME: add Mercurial inputs.
# FIXME: add Mercurial, tarball inputs.
throw "flake input has unsupported input type '${info.type}'";
callFlake4 = flakeSrc: locks:
@ -104,22 +99,7 @@ let
then
let res = builtins.fetchGit src;
in if res.rev == "0000000000000000000000000000000000000000" then removeAttrs res ["rev" "shortRev"] else res
else {
outPath =
# Massage `src` into a store path.
if builtins.isPath src
then
if dirOf (toString src) == builtins.storeDir
# `builtins.storePath` is not available in pure-eval mode.
&& builtins ? currentSystem
then
# If it's already a store path, don't copy it again.
builtins.storePath src
else
"${src}"
else
src;
};
else { outPath = src; };
# NB git worktrees have a file for .git, so we don't check the type of .git
isGit = builtins.pathExists (src + "/.git");
isShallow = builtins.pathExists (src + "/.git/shallow");
@ -128,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
@ -153,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:
@ -160,13 +155,18 @@ 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 "";
outPath = sourceInfo + ((if subdir == "" then "" else "/") + subdir);
flake = import (outPath + "/flake.nix");
flake = import (sourceInfo + (if subdir != "" then "/" else "") + subdir + "/flake.nix");
inputs = builtins.mapAttrs
(inputName: inputSpec: allNodes.${resolveInput inputSpec})
@ -193,21 +193,7 @@ let
outputs = flake.outputs (inputs // { self = result; });
result =
outputs
# We add the sourceInfo attribute for its metadata, as they are
# relevant metadata for the flake. However, the outPath of the
# sourceInfo does not necessarily match the outPath of the flake,
# as the flake may be in a subdirectory of a source.
# This is shadowed in the next //
// sourceInfo
// {
# This shadows the sourceInfo.outPath
inherit outPath;
inherit inputs; inherit outputs; inherit sourceInfo; _type = "flake";
};
result = outputs // sourceInfo // { inherit inputs; inherit outputs; inherit sourceInfo; };
in
if node.flake or true then
assert builtins.isFunction flake.outputs;
@ -226,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 {}

View file

@ -1,6 +0,0 @@
{
description = "Allow flakes to be used with Nix < 2.4";
outputs = { self }: {
};
}