-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
substitute-all.nix: Define in terms of the substitute
bash function
#238636
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,52 @@ | ||
{ stdenvNoCC }: | ||
{ lib | ||
, stdenvNoCC | ||
}: | ||
|
||
args: | ||
/* | ||
Replaces all occurrences of `@varName@` in `src`, where `varName` is an | ||
attribute passed to the `substituteAll` Nix function, writing the result to a | ||
file in the Nix store. | ||
|
||
# see the substituteAll in the nixpkgs documentation for usage and constaints | ||
stdenvNoCC.mkDerivation ({ | ||
name = if args ? name then args.name else baseNameOf (toString args.src); | ||
Example: | ||
|
||
# Writes a text file to the Nix store with contents "Hello, World!" | ||
substituteAll { | ||
src = builtins.toFile "greeting.txt" "Hello, @subject@!"; | ||
subject = "World"; | ||
} | ||
*/ | ||
{ src | ||
, name ? baseNameOf (toString src) | ||
, dir ? null | ||
, isExecutable ? false | ||
, preInstall ? null | ||
, postInstall ? null | ||
, passthru ? { } | ||
, meta ? { } | ||
# The remaining arguments are the variables to replace. | ||
, ... | ||
}@args: | ||
|
||
let | ||
variables = builtins.removeAttrs args [ | ||
"name" "src" "dir" "isExecutable" "preInstall" "postInstall" | ||
"passthru" "meta" | ||
]; | ||
|
||
# { cat = "meow"; dog = "woof"; } -> [ "--subst-var-by" "cat" "meow" "--subst-var-by" "dog" "woof" ] | ||
substitutions = lib.foldlAttrs | ||
(acc: variable: replacement: acc ++ [ "--subst-var-by" variable replacement ]) | ||
[] | ||
variables; | ||
in | ||
|
||
stdenvNoCC.mkDerivation { | ||
builder = ./substitute-all.sh; | ||
inherit (args) src; | ||
inherit name src dir isExecutable preInstall postInstall passthru meta; | ||
|
||
substitutions = lib.toShellVar "substitutions" substitutions; | ||
passAsFile = [ "substitutions" ]; | ||
|
||
preferLocalBuild = true; | ||
allowSubstitutes = false; | ||
} // args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removeAttrs
is still kinda terrible.It'd be nicer to first have a function that does the right thing for all possible key value combinations, and then define
substituteAll
in terms of that function for compatibility.The interface could be something like this when called
(note that I'm also making the
@
s explicit for better grepping)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The above could be done with
--replace
andescapeShellArg
I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My 2c: I think we could have a new interface but at the same time having a better
substituteAll
that does what it is supposed to do would be much better.