Skip to content

Commit

Permalink
Get rid of the original encoding of Nix strings
Browse files Browse the repository at this point in the history
Directly using Nickel's encoding for symbolic strings is as powerful,
and simpler since it means that `nix-s%"blah"%` is a valid Nix string,
while we previously had to explicitly apply the `NixString` contract.
  • Loading branch information
Théophane Hufschmitt committed Sep 29, 2023
1 parent 136b6be commit 78e972e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 27 deletions.
27 changes: 2 additions & 25 deletions lib/contracts.ncl
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ let predicate | doc "Various predicates used to define contracts"
std.is_record x
&& std.record.has_field type_field x
&& x."%{type_field}" == "nixInput",
is_nix_symbolic_string = fun value =>
std.is_record value
&& std.record.has_field "tag" value
&& value.tag == 'SymbolicString
&& std.record.has_field "prefix" value
&& value.prefix == 'nix,
is_nix_string = fun value =>
std.is_record value
&& std.record.has_field type_field value
&& value."%{type_field}" == "nixString",
is_nickel_derivation = fun x =>
std.is_record x
&& std.record.has_field type_field x
Expand All @@ -39,13 +29,6 @@ let predicate | doc "Various predicates used to define contracts"
}
in

let mk_nix_string = fun fs =>
{
"%{type_field}" = "nixString",
fragments = fs,
}
in

{
# Nix may require name, version, etc. to have a certain format, but we're not sure.
# TODO: refine those contracts
Expand Down Expand Up @@ -154,14 +137,8 @@ from the Nix world) or a derivation defined in Nickel.
`lib.import_file`), pure Nickel strings, and maybe more in the future.
"%%
= fun label value =>
# A contract must always be idempotent (be a no-op if applied a second
# time), so we accept something that is already a NixString
if predicate.is_nix_string value || predicate.is_nix_symbolic_string value then
value
# We accept a single string fragment (a plain string, a derivation or a
# Nix path). We normalize it by wrapping it into symbolic string
else if predicate.is_string_fragment value then
nix-s%"%{std.contract.apply NixStringFragment label value}"%
if predicate.is_string_fragment value then
std.contract.apply NixStringFragment label value
else
std.contract.apply NixSymbolicString label value,

Expand Down
4 changes: 2 additions & 2 deletions lib/lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@
value.nix_drv);
in
derivation prepared
else if organistType == "nixString" || (organistType == "" && value.tag or "" == "SymbolicString" && value.prefix or "" == "nix")
then builtins.concatStringsSep "" (builtins.map importFromNickel_ value.fragments)
else if organistType == "nixPath"
then baseDir + "/${value.path}"
else if organistType == "nixInput"
Expand All @@ -116,6 +114,8 @@
lib.getAttrFromPath chosenAttrPath flakeInputs
else if organistType == "nixPlaceholder"
then builtins.placeholder value.output
else if value.tag or "" == "SymbolicString" && value.prefix or "" == "nix"
then builtins.concatStringsSep "" (builtins.map importFromNickel_ value.fragments)
else builtins.mapAttrs (_: importFromNickel_) value
)
else if (type == "list")
Expand Down

0 comments on commit 78e972e

Please sign in to comment.