dockerTools.buildImage Update Env Variable

I am trying to update the path environment variable on a layer I am building, but it doesn’t look like it is possible:

pkgs.dockerTools.buildImage {
      name = "flutter-env";
      fromImage = fromImage;
      config = {
        Env = [
          "FLUTTER_HOME=${FLUTTER_HOME}" # Works
          "PATH=\${PATH}:${FLUTTER_HOME}/bin:${FLUTTER_HOME}/bin/cache/dart-sdk/bin"# Does not work
          ];
      };
};

${PATH} Does not resolve to the previous path, but instead to literal ${PATH}. I can’t just override PATH because I need the path from the previous images. I know this is easily done in a dockerfile and dockerTools config follows the 1.2 spec https://github.com/moby/moby/blob/daa4618da826fb1de4fc2478d88196edbba49b2f/image/spec/v1.2.md
So I hope there is a solution.

I assume what you want here is to simply use bash’s variable interpretation at runtime (${PATH}). That’s also the syntax for string interpolation in Nix though, so what’s actually happening is that Nix tries to resolve the variable PATH which isn’t defined. Because of that, your code snippet should give you an evaluation error though, not insert the literal string ${PATH}.

If my assumption is correct, you must escape it using ''$... like this:

"PATH=''${PATH}:${FLUTTER_HOME}/bin:${FLUTTER_HOME}/bin/cache/dart-sdk/bin"# Should work

That would be the case if this was a multi-line string. It is a single line string so \$ is correct. I get error: undefined variable 'PATH' with the example you provided. I am able to verify that the literal ${PATH} value is on path by adding /bin (so I can still access bash).

"PATH=/bin:\${PATH}:${FLUTTER_HOME}/bin:${FLUTTER_HOME}/bin/cache/dart-sdk/bin"

so using the \${PATH} as above yields

bash-5.2# echo $PATH
/bin:${PATH}:/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin
bash-5.2# 

Some more attempts:

$${PATH} yields

/bin:$${PATH}:/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin

${"PATH"} yields

/bin:PATH:/usr/local/flutter/bin:/usr/local/flutter/bin/cache/dart-sdk/bin

I ended up creating a ticket: dockerTools.buildImage Cannot Append to Environment Variables · Issue #277750 · NixOS/nixpkgs · GitHub