Skip to content

Commit

Permalink
feat: make importing the std library optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nrdxp committed Aug 5, 2024
1 parent 60dd3b8 commit 7b3cb60
Show file tree
Hide file tree
Showing 33 changed files with 105 additions and 22 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ jobs:
- uses: actions/checkout@v4
- uses: nixbuild/nix-quick-install-action@v28
- name: Structural Integirty Test
run: cd test && ./bld.sh
run: cd test/integrity && ./bld.sh
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: nixbuild/nix-quick-install-action@v28
- name: Code Format Check
run: eval "$(nix print-dev-env -f shell.nix)" && treefmt --fail-on-change
std-config:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: nixbuild/nix-quick-install-action@v28
- name: Std Library Config Test
run: cd test/std-import && ./import.sh > /dev/null
3 changes: 3 additions & 0 deletions compose.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[std]
use = true
nixpkgs_lib = false
46 changes: 33 additions & 13 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
let
src = import ./src;
l = builtins;
pins = import ./npins;
in
{
extern ? { },
config ? builtins.fromTOML (builtins.readFile ./compose.toml),
}:
dir:
with src;
Expand All @@ -20,20 +22,38 @@ let

contents = l.readDir dir;

scope = injectPrevious pre {
inherit std;
atom = atom';
mod = self';
builtins = errors.builtins;
import = errors.import;
scopedImport = errors.import;
__fetchurl = errors.fetch;
__currentSystem = errors.system;
__currentTime = errors.time;
__nixPath = errors.nixPath;
__storePath = errors.storePath;
preOpt = {
_if = pre != null;
inherit pre;
};

scope =
injectOptionals
{
atom = atom';
mod = self';
builtins = errors.builtins;
import = errors.import;
scopedImport = errors.import;
__fetchurl = errors.fetch;
__currentSystem = errors.system;
__currentTime = errors.time;
__nixPath = errors.nixPath;
__storePath = errors.storePath;
}
[
preOpt
{
_if = config.std.use or false;
std =
std
// cond {
_if = config.std.nixpkgs_lib or false;
lib = import "${pins."nixpkgs.lib"}/lib";
};
}
];

Import = scopedImport scope;

g =
Expand All @@ -43,7 +63,7 @@ let
file = parse name;
in
if type == "directory" then
{ ${name} = f (injectPrevious pre (lowerKeys self)) path; }
{ ${name} = f ((lowerKeys self) // cond preOpt) path; }
else if type == "regular" && file.ext or null == "nix" && name != "mod.nix" then
{ ${file.name} = Import "${path}"; }
else
Expand Down
12 changes: 12 additions & 0 deletions npins/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
"name": "nixpkgs-unstable",
"url": "https://releases.nixos.org/nixpkgs/nixpkgs-24.11pre660607.c3392ad349a5/nixexprs.tar.xz",
"hash": "1g2p74w085252qbxjymrzzccd203bfwxh8sbcyvbzan6bgqfvwi1"
},
"nixpkgs.lib": {
"type": "Git",
"repository": {
"type": "GitHub",
"owner": "nix-community",
"repo": "nixpkgs.lib"
},
"branch": "master",
"revision": "8bebd4c74f368aacb047f0141db09ec6b339733c",
"url": "https://github.com/nix-community/nixpkgs.lib/archive/8bebd4c74f368aacb047f0141db09ec6b339733c.tar.gz",
"hash": "0vzy9hs0k8zi9h4lx1krq578j3wdc62d6g4hddxj9sfkdmz8r3bn"
}
},
"version": 3
Expand Down
8 changes: 1 addition & 7 deletions src/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,7 @@ in
${toString dir}/mod.nix
'';

injectPrevious =
pre: set:
set
// cond {
_if = pre != null;
inherit pre;
};
injectOptionals = builtins.foldl' (acc: x: acc // cond x);

hasMod = contents: contents."mod.nix" or null == "regular";
}
7 changes: 7 additions & 0 deletions test/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
should_fail () {
if "$@" 2> /dev/null; then
exit 1
else
true
fi
}
2 changes: 1 addition & 1 deletion test/bld.nix → test/integrity/bld.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let
compose = import ../.;
compose = import ../../.;
mod = compose { } (
# added to test implicit path conversion when path is a string
builtins.toPath ./bld
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions test/std-import/import.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
let
compose = set: (import ../../.) set ./import;
in
{
default = compose { };
noStd = compose { config.std.use = false; };
withNixpkgsLib = compose {
config.std.nixpkgs_lib = true;
config.std.use = true;
};
noStdNixpkgs = compose {
config.std.nixpkgs_lib = true;
config.std.use = false;
};
}
21 changes: 21 additions & 0 deletions test/std-import/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -ex

. ../common.sh

# defaults
nix eval -f import.nix default.std
should_fail nix eval -f import.nix default.lib

# no std set
should_fail nix eval -f import.nix noStd.std
should_fail nix eval -f import.nix noStd.lib

# no std set
nix eval -f import.nix withNixpkgsLib.std
nix eval -f import.nix withNixpkgsLib.lib

# no std set
should_fail nix eval -f import.nix noStdNixpkgs.std
should_fail nix eval -f import.nix noStdNixpkgs.lib
4 changes: 4 additions & 0 deletions test/std-import/import/mod.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
Std = std;
Lib = std.lib;
}

0 comments on commit 7b3cb60

Please sign in to comment.