agetty: Add autologinUser config option

This option causes the specified user to be automatically logged in at
the virtual console.

While at it, refactor and make a helper function for building the getty
command line.

+17 -6
+17 -6
nixos/modules/services/ttys/agetty.nix
··· 10 10 11 11 services.mingetty = { 12 12 13 + autologinUser = mkOption { 14 + type = types.nullOr types.str; 15 + default = null; 16 + description = '' 17 + Username of the account that will be automatically logged in at the console. 18 + If unspecified, a login prompt is shown as usual. 19 + ''; 20 + }; 21 + 13 22 greetingLine = mkOption { 14 23 type = types.str; 15 24 default = ''<<< Welcome to NixOS ${config.system.nixosVersion} (\m) - \l >>>''; ··· 46 55 47 56 ###### implementation 48 57 49 - config = { 50 - 58 + config = let 59 + autologinArg = optionalString (config.services.mingetty.autologinUser != null) "--autologin ${config.services.mingetty.autologinUser}"; 60 + gettyCmd = extraArgs: "@${pkgs.utillinux}/sbin/agetty agetty --login-program ${pkgs.shadow}/bin/login ${autologinArg} ${extraArgs}"; 61 + in { 51 62 systemd.services."getty@" = 52 - { serviceConfig.ExecStart = "@${pkgs.utillinux}/sbin/agetty agetty --noclear --login-program ${pkgs.shadow}/bin/login --keep-baud %I 115200,38400,9600 $TERM"; 63 + { serviceConfig.ExecStart = gettyCmd "--noclear --keep-baud %I 115200,38400,9600 $TERM"; 53 64 restartIfChanged = false; 54 65 }; 55 66 56 67 systemd.services."serial-getty@" = 57 68 { serviceConfig.ExecStart = 58 69 let speeds = concatStringsSep "," (map toString config.services.mingetty.serialSpeed); 59 - in "@${pkgs.utillinux}/sbin/agetty agetty --login-program ${pkgs.shadow}/bin/login %I ${speeds} $TERM"; 70 + in gettyCmd "%I ${speeds} $TERM"; 60 71 restartIfChanged = false; 61 72 }; 62 73 63 74 systemd.services."container-getty@" = 64 75 { unitConfig.ConditionPathExists = "/dev/pts/%I"; # Work around being respawned when "machinectl login" exits. 65 - serviceConfig.ExecStart = "@${pkgs.utillinux}/sbin/agetty agetty --noclear --login-program ${pkgs.shadow}/bin/login --keep-baud pts/%I 115200,38400,9600 $TERM"; 76 + serviceConfig.ExecStart = gettyCmd "--noclear --keep-baud pts/%I 115200,38400,9600 $TERM"; 66 77 restartIfChanged = false; 67 78 }; 68 79 69 80 systemd.services."console-getty" = 70 - { serviceConfig.ExecStart = "@${pkgs.utillinux}/sbin/agetty agetty --noclear --login-program ${pkgs.shadow}/bin/login --keep-baud console 115200,38400,9600 $TERM"; 81 + { serviceConfig.ExecStart = gettyCmd "--noclear --keep-baud console 115200,38400,9600 $TERM"; 71 82 serviceConfig.Restart = "always"; 72 83 restartIfChanged = false; 73 84 enable = mkDefault config.boot.isContainer;