Skip to content

Commit a37fe3a

Browse files
committed
Introduce file inputs and a whole dev env/tests
The goal here is that we have an ability to develop flake-compat and actually have tests and be able to validate it is correct. This also imports the file input stuff originally written by matthewbauer here: NixOS/flake-compat#44 This bore fruit already in the form of finding a years old bug lying around in Lix: https://git.lix.systems/lix-project/lix/issues/750
1 parent f7a7752 commit a37fe3a

File tree

15 files changed

+347
-3
lines changed

15 files changed

+347
-3
lines changed

.envrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source_env_if_exists .envrc.local
2+
use flake ./dev#

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# generated by nix
2+
.pre-commit-config.yaml

default.nix

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,51 @@ let
1616

1717
lockFile = builtins.fromJSON (builtins.readFile lockFilePath);
1818

19+
# Using custom fetchurl function here so that we can specify outputHashMode.
20+
# The hash we get from the lock file is using recursive ingestion even though
21+
# it’s not unpacked. So builtins.fetchurl and import <nix/fetchurl.nix> are
22+
# insufficient.
23+
# Note that this will be a derivation and not a path as fetchTarball is,
24+
# causing the hash of this input to be different on flake and non-flake evaluation.
25+
#
26+
# Note: there are a bunch of related bugs and it seems like this being
27+
# recursive-hashed to begin with was a mistake:
28+
# https://git.lix.systems/lix-project/lix/issues/750
29+
fetchurl =
30+
{ url, sha256 }:
31+
derivation {
32+
builder = "builtin:fetchurl";
33+
34+
name = "source";
35+
inherit url;
36+
37+
outputHash = sha256;
38+
outputHashAlgo = "sha256";
39+
outputHashMode = "recursive";
40+
executable = false;
41+
unpack = false;
42+
43+
system = "builtin";
44+
45+
# No need to double the amount of network traffic
46+
preferLocalBuild = true;
47+
48+
impureEnvVars = [
49+
# We borrow these environment variables from the caller to allow
50+
# easy proxy configuration. This is impure, but a fixed-output
51+
# derivation like fetchurl is allowed to do so since its result is
52+
# by definition pure.
53+
"http_proxy"
54+
"https_proxy"
55+
"ftp_proxy"
56+
"all_proxy"
57+
"no_proxy"
58+
];
59+
60+
# To make "nix-prefetch-url" work.
61+
urls = [ url ];
62+
};
63+
1964
fetchTree =
2065
info:
2166
if info.type == "github" then
@@ -58,14 +103,16 @@ let
58103
)
59104
else if info.type == "path" then
60105
{
61-
outPath = builtins.path { path = info.path; };
62-
narHash = info.narHash;
106+
outPath = builtins.path (
107+
{ path = info.path; } // (if info ? narHash then { sha256 = info.narHash; } else { })
108+
);
63109
}
64110
else if info.type == "tarball" then
65111
{
66112
outPath = fetchTarball (
67113
{ inherit (info) url; } // (if info ? narHash then { sha256 = info.narHash; } else { })
68114
);
115+
narHash = info.narHash;
69116
}
70117
else if info.type == "gitlab" then
71118
{
@@ -89,6 +136,24 @@ let
89136
);
90137
shortRev = builtins.substring 0 7 info.rev;
91138
}
139+
else if info.type == "file" then
140+
{
141+
outPath =
142+
if
143+
builtins.substring 0 7 info.url == "http://" || builtins.substring 0 8 info.url == "https://"
144+
then
145+
fetchurl ({ inherit (info) url; } // (if info ? narHash then { sha256 = info.narHash; } else { }))
146+
else if builtins.substring 0 7 info.url == "file://" then
147+
builtins.path (
148+
{
149+
path = builtins.substring 7 (-1) info.url;
150+
}
151+
// (if info ? narHash then { sha256 = info.narHash; } else { })
152+
)
153+
else
154+
throw "can't support url scheme of flake input with url '${info.url}'";
155+
narHash = info.narHash;
156+
}
92157
else
93158
# FIXME: add Mercurial inputs.
94159
throw "flake input has unsupported input type '${info.type}'";

dev/flake.lock

Lines changed: 78 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dev/flake.nix

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Separate flake for validation, since we don't want to make these visible to users
2+
{
3+
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
4+
inputs.flake-utils.url = "github:numtide/flake-utils";
5+
inputs.pre-commit-hooks = {
6+
url = "github:cachix/git-hooks.nix";
7+
flake = false;
8+
};
9+
10+
outputs =
11+
{
12+
self,
13+
nixpkgs,
14+
flake-utils,
15+
pre-commit-hooks,
16+
}:
17+
(flake-utils.lib.eachDefaultSystem (
18+
system:
19+
let
20+
pkgs = import nixpkgs { inherit system; };
21+
checks = self.checks.${system};
22+
in
23+
{
24+
devShells.default = pkgs.mkShell {
25+
shellHook = ''
26+
${nixpkgs.lib.optionalString (checks.pre-commit ? shellHook) checks.pre-commit.shellHook}
27+
'';
28+
nativeBuildInputs = with pkgs.buildPackages; [
29+
treefmt
30+
nixfmt-rfc-style
31+
ruff
32+
33+
python3
34+
python3Packages.pytest
35+
];
36+
};
37+
38+
checks.pre-commit =
39+
let
40+
# Avoid an unnecessary reimport of nixpkgs
41+
tools = import (pre-commit-hooks + "/nix/call-tools.nix") pkgs;
42+
pre-commit-run = pkgs.callPackage (pre-commit-hooks + "/nix/run.nix") {
43+
inherit tools;
44+
isFlakes = true;
45+
# unused!
46+
gitignore-nix-src = builtins.throw "gitignore-nix-src is unused";
47+
};
48+
in
49+
pre-commit-run {
50+
src = ../.;
51+
hooks = {
52+
treefmt = {
53+
enable = true;
54+
settings.formatters = with pkgs.buildPackages; [
55+
ruff
56+
nixfmt-rfc-style
57+
];
58+
};
59+
};
60+
};
61+
}
62+
));
63+
}

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
description = "Allow flakes to be used with Nix < 2.4";
2+
description = "Allow flakes to be evaluated from non-flake contexts";
33

44
outputs =
55
{ self }:

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[project]
2+
name = "flake-compat"
3+
version = "0.0.0"
4+
dependencies = [
5+
"pytest",
6+
]

tests/__init__.py

Whitespace-only changes.

tests/file_input/__init__.py

Whitespace-only changes.

tests/file_input/default.nix

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
flake-compat ? ../../default.nix,
3+
}:
4+
(import flake-compat {
5+
src = ./.;
6+
}).defaultNix

0 commit comments

Comments
 (0)