Unverified Commit 0f9f91c4 authored by nikstur's avatar nikstur Committed by GitHub
Browse files

nixos-init: extend PATH with config instead of overriding (#491409)

parents dbae9a68 e2b96c6c
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -713,7 +713,12 @@ in
    systemd.managerEnvironment = {
      # Doesn't contain systemd itself - everything works so it seems to use the compiled-in value for its tools
      # util-linux is needed for the main fsck utility wrapping the fs-specific ones
      PATH = lib.makeBinPath (config.system.fsPackages ++ [ cfg.package.util-linux ]);
      PATH = lib.makeBinPath (
        config.system.fsPackages
        ++ [ cfg.package.util-linux ]
        # systemd-ssh-generator needs sshd in PATH
        ++ lib.optional config.services.openssh.enable config.services.openssh.package
      );
      LOCALE_ARCHIVE = "/run/current-system/sw/lib/locale/locale-archive";
      TZDIR = "/etc/zoneinfo";
      # If SYSTEMD_UNIT_PATH ends with an empty component (":"), the usual unit load path will be appended to the contents of the variable
+12 −1
Original line number Diff line number Diff line
@@ -18,12 +18,23 @@ struct Config(HashMap<String, String>);
/// Reads the JSON config for the systemd generator environment and prints it in KEY=VALUE format
/// to stdout. This makes the configured environment variables available for all systemd
/// generators.
///
/// In case of the PATH variable, it extends the PATH read from the environment with the one from
/// the config.
fn env_generator_impl() -> Result<()> {
    let content = fs::read(CONFIG_PATH).with_context(|| format!("Failed to read {CONFIG_PATH}"))?;
    let config: Config = serde_json::from_slice(&content).context("Failed to parse config")?;

    let mut buffer = Vec::new();
    for (key, value) in config.0 {
    for (key, mut value) in config.0 {
        // If the config contains the PATH env variable, read the current PATH and extend it with
        // the one from the config.
        if key == "PATH"
            && let Some(current_path) = std::env::var("PATH").ok()
        {
            value.push(':');
            value.push_str(&current_path);
        }
        writeln!(&mut buffer, "{key}=\"{value}\"").context("Failed to write to buffer")?;
    }