From f072535eb2847cf3e4606b2e6dd62231c3322c65 Mon Sep 17 00:00:00 2001 From: adisbladis Date: Thu, 28 May 2026 15:43:43 +1200 Subject: [PATCH] nixos/pam: Write all pam.d config in a single derivation Calling writeText in a loop is silly when we can use a single runCommand by passing all config as structured attrs. --- nixos/modules/security/pam.nix | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/nixos/modules/security/pam.nix b/nixos/modules/security/pam.nix index 6cd174fabc2b..6012b47c16e2 100644 --- a/nixos/modules/security/pam.nix +++ b/nixos/modules/security/pam.nix @@ -1802,11 +1802,6 @@ let else config.users.motdFile; - makePAMService = name: service: { - name = "pam.d/${name}"; - value.source = pkgs.writeText "${name}.pam" service.text; - }; - optionalSudoConfigForSSHAgentAuth = lib.optionalString (config.security.pam.sshAgentAuth.enable || config.security.pam.rssh.enable) '' @@ -2594,7 +2589,26 @@ in }; }; - environment.etc = lib.mapAttrs' makePAMService enabledServices; + environment.etc = + let + # Write all pam config in a single derivation for performance + pamd = + pkgs.runCommand "pam.d" + { + __structuredAttrs = true; + services = lib.mapAttrs (_: svc: svc.text) enabledServices; + } + '' + mkdir $out + for i in "''${!services[@]}"; do + printf '%s' "''${services[$i]}" > "$out/$i" + done + ''; + in + lib.mapAttrs' (name: service: { + name = "pam.d/${name}"; + value.source = "${pamd}/${name}"; + }) enabledServices; systemd = lib.mkIf (lib.any (service: service.lastlog.enable) (lib.attrValues config.security.pam.services))