-
-
Notifications
You must be signed in to change notification settings - Fork 14k
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
toShell*: turn nix data-structures into its shell equivalents #147551
Conversation
85b714d
to
e8bee9d
Compare
That toShellMap = attrs:
"(${
concatStringsSep " " (mapAttrsToList
(key: val: "[${escapeShellArg key}]=${escapeShellArg val}") attrs)
})"; Usage: ''
declare -A myMap=${toShellMap myMap}
'' |
I find your proposal very interesting, I also added two flavors of Let me explain why this is a good idea There are two widely used ways of calling
If we keep only nixpkgs.stdenv.mkDerivation {
name = "example";
builder = ./builder.sh;
# Unnecessary wrapper, can be abstracted with `toShellMapFile`
myMapSetup = builtins.toFile "setup" ''
declare -A myMap=${toShellMap testData}
'';
} source $stdenv/setup
source $myMapSetup
for key in "${!myMap[@]}"; do
val=''${myMap[$key]}
echo $key: $val
done Now, with nixpkgs.stdenv.mkDerivation {
name = "example";
builder = ./builder.sh;
# Minimal code
myMapSetup = toShellMapFile "myMap" testData;
} source $stdenv/setup
source $myMapSetup
for key in "${!myMap[@]}"; do
val="${myMap[$key]}"
echo $key: $val
done and nixpkgs.stdenv.mkDerivation {
name = "example";
# Minimal code, also
builder = builtins.toFile "builder" ''
declare -A myMap=${toShellMap testData}
'';
} covering common uses of |
Even in the external builder case, instead of |
env vars length, CLI arguments count, and others, have hard limits examplelet
testData = genAttrs (map toString (range 1 10000)) (name: "value");
in
nixpkgs.stdenv.mkDerivation {
name = "example";
builder = ./builder.sh;
someVarSetup = toShellMap testData;
} this is why files (example: passAsFile) come into play, they allow people to overcome those limits by encapsulading lots of data into small identifiers now, this do happen in real life, for example:
anyway, if people prefer and their maps are small, they can use
For big maps or people who do not like using |
For big maps, |
@andersk The PR was updated to be exactly as you propose Thanks for the feedback |
Isn't structuredAttrs supposed this problem, well, structurally? (Excuse the bad pun) |
@roberth I don't think
On the other hand, this PR gets the job done, today, with docs: |
Motivation for this change
Bash's data-structures are quite limited, mapping from Nix data structures into Bash ones can be sometimes tricky. By adding this built-in we enhance our scripts capabilities in a very clean way
Things done
sandbox = true
set innix.conf
? (See Nix manual)nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD"
. Note: all changes have to be committed, also see nixpkgs-review usage./result/bin/
)nixos/doc/manual/md-to-db.sh
to update generated release notes