Compare commits

..

5 commits

Author SHA1 Message Date
Matthew Bauer c1217c6a4e Add note 2022-08-29 13:39:54 -05:00
Matthew Bauer b1d2eff40a Discard context on fetchurl derivation
so that fetchurl outputs a real path instead of a source, and our
hashes match correctly between flake and non-flake.

also it looks like fetchTarball can support local files
2022-08-29 12:00:24 -05:00
Matthew Bauer 0b194c44aa Add hash to builtins.path calls 2022-08-26 15:06:04 -05:00
Matthew Bauer 6e1fbe397a Support local files too
Add support for local files in addition to http / https.
2022-08-26 15:03:10 -05:00
Matthew Bauer 5932243fcc Support fetching plain files
Add supports for new type = "file" locks added in
5b8c1deb18.

Unfortunately, the hash provided by the lock file is a recursive hash,
while fetchurl wants a flat hash. So we have to use a custom fetchurl
function using the builtin:fetchurl builder. Hopefully okay just to
add compat for this very useful lock type.
2022-08-26 14:42:35 -05:00
3 changed files with 65 additions and 68 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,7 @@ 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 = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)

View file

@ -13,6 +13,42 @@ let
lockFile = builtins.fromJSON (builtins.readFile lockFilePath);
# Using custom fetchurl function here so that we can specify outputHashMode.
# The hash we get from the lock file is using recursive ingestion even though
# its not unpacked. So builtins.fetchurl and import <nix/fetchurl.nix> are
# insufficient.
# Note that this will be a derivation and not a path as fetchTarball is,
# causing the hash of this input to be different on flake and non-flake evaluation.
fetchurl = { url, sha256 }:
derivation {
builder = "builtin:fetchurl";
name = "source";
inherit url;
outputHash = sha256;
outputHashAlgo = "sha256";
outputHashMode = "recursive";
executable = false;
unpack = false;
system = "builtin";
# No need to double the amount of network traffic
preferLocalBuild = true;
impureEnvVars = [
# We borrow these environment variables from the caller to allow
# easy proxy configuration. This is impure, but a fixed-output
# derivation like fetchurl is allowed to do so since its result is
# by definition pure.
"http_proxy" "https_proxy" "ftp_proxy" "all_proxy" "no_proxy"
];
# To make "nix-prefetch-url" work.
urls = [ url ];
};
fetchTree =
info:
if info.type == "github" then
@ -44,7 +80,10 @@ let
} else {
})
else if info.type == "path" then
{ outPath = builtins.path { path = info.path; };
{ outPath = builtins.path
({ path = info.path; }
// (if info ? narHash then { sha256 = info.narHash; } else {})
);
narHash = info.narHash;
}
else if info.type == "tarball" then
@ -53,6 +92,7 @@ let
({ inherit (info) url; }
// (if info ? narHash then { sha256 = info.narHash; } else {})
);
narHash = info.narHash;
}
else if info.type == "gitlab" then
{ inherit (info) rev narHash lastModified;
@ -63,17 +103,23 @@ 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 if info.type == "file" then
{ outPath =
if builtins.substring 0 7 info.url == "http://" || builtins.substring 0 8 info.url == "https://" then
fetchurl
({ inherit (info) url; }
// (if info ? narHash then { sha256 = info.narHash; } else {})
)
else if builtins.substring 0 7 info.url == "file://" then
builtins.path
({ path = builtins.substring 7 (-1) info.url; }
// (if info ? narHash then { sha256 = info.narHash; } else {})
)
else throw "can't support url scheme of flake input with url '${info.url}'";
narHash = info.narHash;
}
else
# FIXME: add Mercurial inputs.
# FIXME: add Mercurial input
throw "flake input has unsupported input type '${info.type}'";
callFlake4 = flakeSrc: locks:
@ -104,22 +150,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");
@ -164,9 +195,7 @@ let
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 +222,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;
@ -229,7 +244,7 @@ let
in
rec {
defaultNix =
(builtins.removeAttrs result ["__functor"])
result
// (if result ? defaultPackage.${system} then { default = result.defaultPackage.${system}; } else {})
// (if result ? packages.${system}.default then { default = result.packages.${system}.default; } else {});

View file

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