-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
Description
Description
In pkgs.writeShellApplication, when the runtimeEnv attrset includes a string attribute that could be interpreted as a command, it fails to build because of SC2209.
{ pkgs ? import <nixpkgs> {} }: pkgs.writeShellApplication {
name = "test";
runtimeEnv = {
TEST_VAR = "ls";
};
text = ''echo "$TEST_VAR"'';
}SC2209 (warning): Use var=$(command) to assign output (or quote to assign string).Steps To Reproduce
- Copy the following into a terminal with Nix installed.
nix-build - << EOF
{ pkgs ? import <nixpkgs> {} }: pkgs.writeShellApplication {
name = "test";
runtimeEnv = {
TEST_VAR = "ls";
};
text = ''echo "$TEST_VAR"'';
}
EOF- Press enter to run the command.
- Observe
SC2209.
Expected behavior
The above script should successfully build. When run, the script would print "ls" to the console.
Screenshots
N/A
Additional context
pkgs.writeShellApplication uses lib.toShellVar to generate the variable in the shell script.
- See
pkgs.writeShellApplicationat pkgs/build-support/trivial-builders/default.nix. - See
lib.toShellVarat lib/strings.nix. - See
SC2209at shellcheck.net/wiki/SC2209.
For sure, the solution is to modify the generated shell script such that:
TEST_VAR=ls -> TEST_VAR="ls"
This change must occur within either pkgs.writeShellApplication or lib.toShellVar.
I believe it would be best for the solution to be within the lib.toShellVar definition.
Perhaps the solution is as easy as adding quotes around the variable.
I believe Nix requires the input of toShellVar to be a string anyways.
If not, additional edge cases must be considered for each datatype that could be passed.
Metadata
- system: `"x86_64-linux"`
- host os: `Linux 6.6.66, NixOS, 24.11 (Vicuna), 24.11.20241216.3945713`
- multi-user?: `yes`
- sandbox: `yes`
- version: `nix-env (Nix) 2.24.10`
- nixpkgs: `/nix/store/wj2qla569hnxwqfc26imv5hqbxc1rc27-source`Notify maintainers
lib.toShellVar@ncfavier @roberthpkgs.writeShellApplication@lovesegfault @9999years
Note for maintainers: Please tag this issue in your PR.
Add a 👍 reaction to issues you find important.