Skip to content

Commit

Permalink
Generate the lockfile on the fly when entering the shell
Browse files Browse the repository at this point in the history
Avoids the need for an explicit `nix run .#regenerate-lockfile`
  • Loading branch information
Théophane Hufschmitt committed Jun 21, 2024
1 parent 1b4c9ca commit a415c2e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 33 deletions.
8 changes: 1 addition & 7 deletions lib/files.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ let NormaliseTargets = fun label files =>
in
let regenerate_files | Files -> nix.derivation.Derivation = fun files_to_generate =>
let regnerate_one | String -> File -> nix.derivation.NixString = fun key file_descr =>
let file_content = file_descr.content in
let target = file_descr.target in
let copy_command =
match {
Expand All @@ -54,11 +53,6 @@ let regenerate_files | Files -> nix.derivation.Derivation = fun files_to_generat
}
file_descr.materialisation_method
in
let file_in_store =
nix.builtins.to_file
(nix.utils.escape_drv_name key)
file_content
in
nix-s%"
rm -f %{target}
echo "Regenerating %{target}"
Expand All @@ -67,7 +61,7 @@ let regenerate_files | Files -> nix.derivation.Derivation = fun files_to_generat
# XXX: If `source.file` is set explicitely to a relative path
# and `materialisation_method` is `'Symlink`, this will link to the
# original file, not one in the store. Not sure that's what we want.
%{copy_command} %{file_in_store} %{target}
%{copy_command} %{file_descr.file} %{target}
"%
in
{
Expand Down
37 changes: 19 additions & 18 deletions lib/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -169,23 +169,19 @@
passAsFile = ["expectedLockfileContents"];
} (
if needNewLockfile
then
lib.warn ''
Lockfile contents are outdated. Please run "nix run .#regenerate-lockfile" to update them.
''
''
cp -r "${sources}" sources
if [ -f sources/nickel.lock.ncl ]; then
chmod +w sources sources/nickel.lock.ncl
else
chmod +w sources
fi
cp $expectedLockfileContentsPath sources/nickel.lock.ncl
cat > eval.ncl <<EOF
${nickelWithImports "sources"}
EOF
${nickel}/bin/nickel export eval.ncl --field config.flake > $out
''
then ''
cp -r "${sources}" sources
if [ -f sources/nickel.lock.ncl ]; then
chmod +w sources sources/nickel.lock.ncl
else
chmod +w sources
fi
cp $expectedLockfileContentsPath sources/nickel.lock.ncl
cat > eval.ncl <<EOF
${nickelWithImports "sources"}
EOF
${nickel}/bin/nickel export eval.ncl --field config.flake > $out
''
else ''
cat > eval.ncl <<EOF
${nickelWithImports sources}
Expand All @@ -212,9 +208,14 @@
nickelResult = callNickel {
inherit nickelFile baseDir flakeInputs lockFileContents;
};
enrichedFlakeInputs =
flakeInputs
// {
"%%organist_internal".nickelLock = builtins.toFile "nickel.lock.ncl" (buildLockFileContents lockFileContents);
};
in
{rawNickel = nickelResult;}
// (importFromNickel flakeInputs system baseDir (builtins.fromJSON
// (importFromNickel enrichedFlakeInputs system baseDir (builtins.fromJSON
(builtins.unsafeDiscardStringContext (builtins.readFile nickelResult))));
in {
inherit
Expand Down
8 changes: 8 additions & 0 deletions lib/lockfile.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
let filegen = import "files.ncl" in
let nix = import "nix-interop/nix.ncl" in
{
Schema = filegen.Schema,
config | Schema = {
files."nickel.lock.ncl".file = nix.import_nix "%%organist_internal#nickelLock",
}
}
2 changes: 2 additions & 0 deletions lib/schema.ncl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let nix = import "./nix-interop/nix.ncl" in
let filegen = import "files.ncl" in
let lockfile = import "lockfile.ncl" in
{
OrganistShells = {
dev
Expand Down Expand Up @@ -70,4 +71,5 @@ let filegen = import "files.ncl" in
},
}
& filegen
& lockfile
}
13 changes: 5 additions & 8 deletions run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,24 @@ test_one_template () (
sed -i "s/shells\.Bash/shells.$target/" project.ncl
prepare_shell

STORED_LOCKFILE_CONTENTS="$(cat nickel.lock.ncl)"
TEST_SCRIPT="$(nickel export --format raw <<<'(import "'"$PROJECT_ROOT"'/lib/shell-tests.ncl").'"$target"'.script')"

echo "Running with incorrect nickel.lock.ncl" 1>&2
echo '{}' > nickel.lock.ncl
nix develop --accept-flake-config --print-build-logs --command bash <<<"$TEST_SCRIPT"

if [[ $isFull == false ]]; then
return
fi

echo "Running without nickel.lock.ncl" 1>&2
rm nickel.lock.ncl
rm -f nickel.lock.ncl
nix develop --accept-flake-config --print-build-logs --command bash <<<"$TEST_SCRIPT"

echo "Run with proper nickel.lock.ncl" 1>&2
nix run .\#regenerate-lockfile
PROPER_LOCKFILE_CONTENTS="$(cat nickel.lock.ncl)"
nix develop --accept-flake-config --print-build-logs --command bash <<<"$TEST_SCRIPT"

echo "Testing without flakes" 1>&2
# restore lockfile
cat > nickel.lock.ncl <<<"$STORED_LOCKFILE_CONTENTS"
# pretend it's not flake anymore
rm flake.*
cat > shell.nix <<EOF
Expand All @@ -86,14 +82,15 @@ in
EOF

echo "Running with incorrect nickel.lock.ncl" 1>&2
rm -f nickel.lock.ncl
echo '{}' > nickel.lock.ncl
nix develop --impure -f shell.nix -I nixpkgs="$NIXPKGS_PATH" --command bash <<<"$TEST_SCRIPT"

echo "Running without nickel.lock.ncl" 1>&2
rm nickel.lock.ncl
rm -f nickel.lock.ncl
nix develop --impure -f shell.nix -I nixpkgs="$NIXPKGS_PATH" --command bash <<<"$TEST_SCRIPT"

echo "Run with proper nickel.lock.ncl" 1>&2
cat > nickel.lock.ncl <<<"$PROPER_LOCKFILE_CONTENTS"
nix develop --impure -f shell.nix -I nixpkgs="$NIXPKGS_PATH" --command bash <<<"$TEST_SCRIPT"

popd
Expand Down

0 comments on commit a415c2e

Please sign in to comment.