Skip to content

Commit

Permalink
Add lib.to_file mirroring builtins.toFile from Nix
Browse files Browse the repository at this point in the history
Also add scaffolding for adding library tests to checks.
  • Loading branch information
YorikSar committed Sep 29, 2023
1 parent 5a7c295 commit 8c3549b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/contracts.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ let predicate | doc "Various predicates used to define contracts"
std.is_record x
&& std.record.has_field type_field x
&& x."%{type_field}" == "nixPlaceholder",
is_nix_to_file = fun x =>
std.is_record x
&& std.record.has_field type_field x
&& x."%{type_field}" == "nixToFile",
is_nix_input = fun x =>
std.is_record x
&& std.record.has_field type_field x
Expand All @@ -36,6 +40,7 @@ let predicate | doc "Various predicates used to define contracts"
|| std.is_string x
|| is_nix_path x
|| is_nix_placeholder x
|| is_nix_to_file x
}
in

Expand Down Expand Up @@ -305,6 +310,14 @@ from the Nix world) or a derivation defined in Nickel.
output | String,
},

NixToFile
| doc "A path to the given output resolved later in the Nix store"
= {
"%{type_field}" | force = "nixToFile",
name | String,
text | NixString,
},

OrganistShells = {
dev | NickelDerivation = build,
build | NickelDerivation,
Expand Down
4 changes: 3 additions & 1 deletion lib/lib.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ let contracts = import "contracts.ncl" in
import_nix | contracts.NixInputSugar -> contracts.NixInput
= fun x => x,
placeholder | String -> contracts.NixPlaceholder
= fun _output => { output = _output }
= fun _output => { output = _output },
to_file | String -> contracts.NixString -> contracts.NixToFile
= fun _name _text => { name = _name, text = _text },
}
2 changes: 2 additions & 0 deletions lib/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@
lib.getAttrFromPath chosenAttrPath flakeInputs
else if organistType == "nixPlaceholder"
then builtins.placeholder value.output
else if organistType == "nixToFile"
then builtins.toFile value.name (importFromNickel_ value.text)
else builtins.mapAttrs (_: importFromNickel_) value
)
else if (type == "list")
Expand Down
2 changes: 2 additions & 0 deletions project.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ let import_nix = organist.lib.import_nix in
"%,
},
},

flake.checks = import "tests/main.ncl",
}
| organist.contracts.OrganistExpression
3 changes: 3 additions & 0 deletions tests/main.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
to_file = import "to_file.ncl",
}
12 changes: 12 additions & 0 deletions tests/to_file.ncl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let organist = import "../lib/nix.ncl" in
let file1 = organist.lib.to_file "file1" "important data" in
let file2 = organist.lib.to_file "file2" nix-s%"see %{file1}"% in
organist.builders.NixpkgsPkg
& {
name = "test-to_path",
env.buildCommand = nix-s%"
[[ $(cat %{file1}) == "important data" ]]
[[ $(cat %{file2}) == "see /nix/store/ypiiqm7ig0fzqfz3v4j05g54ffk8svg9-file1" ]]
echo OK > $out
"%,
}

0 comments on commit 8c3549b

Please sign in to comment.