NixOS: Pass through environment variables to system.activationScripts

In my configuration.nix I set:

  system.activationScripts.zz-test = {
    text = ''env | sort'';
    supportsDryActivation = true;
  };

I then invoke sudo NO_COLOR=1 nixos-rebuild dry-activate, but env does not list NO_COLOR.

How do I pass through environment variables to system.activationScripts? I do not want to set them statically in the activation script itself, but want to be able to influence the behaviour when I run nixos-rebuild.

Try adding --impure.

I have this in my configuration.nix

   system.activationScripts.diff = {
     supportsDryActivation = true;
     text = ''
       ${pkgs.nvd}/bin/nvd --nix-bin-dir=${pkgs.nix}/bin diff /run/current-system "$systemConfig"
     '';
   };

It tells me what was added, removed, upgraded and size change for software.

You can’t. Activation scripts are ran as an ephemeral systemd service, exactly to avoid external environment causing issues.