Skip to content

writeShellApplication does not quote runtimeEnv strings, triggering SC2209 #367288

@mboyea

Description

@mboyea

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

  1. 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
  1. Press enter to run the command.
  2. 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.

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


Note for maintainers: Please tag this issue in your PR.


Add a 👍 reaction to issues you find important.

Metadata

Metadata

Assignees

No one assigned

    Labels

    0.kind: bugSomething is broken2.status: stalehttps://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions