base16-schemes: unstable-2024-06-21 -> unstable-2024-11-12
[NixPkgs.git] / nixos / modules / services / ttys / getty.nix
blobfd9bd7aca01900a29807d44bb67e947be33f1f97
1 { config, lib, pkgs, ... }:
3 with lib;
5 let
6   cfg = config.services.getty;
8   baseArgs = [
9     "--login-program" "${cfg.loginProgram}"
10   ] ++ optionals (cfg.autologinUser != null && !cfg.autologinOnce) [
11     "--autologin" cfg.autologinUser
12   ] ++ optionals (cfg.loginOptions != null) [
13     "--login-options" cfg.loginOptions
14   ] ++ cfg.extraArgs;
16   gettyCmd = args:
17     "${lib.getExe' pkgs.util-linux "agetty"} ${escapeShellArgs baseArgs} ${args}";
19   autologinScript = ''
20     otherArgs="--noclear --keep-baud $TTY 115200,38400,9600 $TERM";
21     ${lib.optionalString cfg.autologinOnce ''
22       autologged="/run/agetty.autologged"
23       if test "$TTY" = tty1 && ! test -f "$autologged"; then
24         touch "$autologged"
25         exec ${gettyCmd "$otherArgs --autologin ${cfg.autologinUser}"}
26       fi
27     ''}
28     exec ${gettyCmd "$otherArgs"}
29   '';
35   ###### interface
37   imports = [
38     (mkRenamedOptionModule [ "services" "mingetty" ] [ "services" "getty" ])
39     (mkRemovedOptionModule [ "services" "getty" "serialSpeed" ] ''set non-standard baudrates with `boot.kernelParams` i.e. boot.kernelParams = ["console=ttyS2,1500000"];'')
40   ];
42   options = {
44     services.getty = {
46       autologinUser = mkOption {
47         type = types.nullOr types.str;
48         default = null;
49         description = ''
50           Username of the account that will be automatically logged in at the console.
51           If unspecified, a login prompt is shown as usual.
52         '';
53       };
55       autologinOnce = mkOption {
56         type = types.bool;
57         default = false;
58         description = ''
59           If enabled the automatic login will only happen in the first tty
60           once per boot. This can be useful to avoid retyping the account
61           password on systems with full disk encrypted.
62         '';
63       };
65       loginProgram = mkOption {
66         type = types.path;
67         default = "${pkgs.shadow}/bin/login";
68         defaultText = literalExpression ''"''${pkgs.shadow}/bin/login"'';
69         description = ''
70           Path to the login binary executed by agetty.
71         '';
72       };
74       loginOptions = mkOption {
75         type = types.nullOr types.str;
76         default = null;
77         description = ''
78           Template for arguments to be passed to
79           {manpage}`login(1)`.
81           See {manpage}`agetty(1)` for details,
82           including security considerations.  If unspecified, agetty
83           will not be invoked with a {option}`--login-options`
84           option.
85         '';
86         example = "-h darkstar -- \\u";
87       };
89       extraArgs = mkOption {
90         type = types.listOf types.str;
91         default = [ ];
92         description = ''
93           Additional arguments passed to agetty.
94         '';
95         example = [ "--nohostname" ];
96       };
98       greetingLine = mkOption {
99         type = types.str;
100         description = ''
101           Welcome line printed by agetty.
102           The default shows current NixOS version label, machine type and tty.
103         '';
104       };
106       helpLine = mkOption {
107         type = types.lines;
108         default = "";
109         description = ''
110           Help line printed by agetty below the welcome line.
111           Used by the installation CD to give some hints on
112           how to proceed.
113         '';
114       };
116     };
118   };
121   ###### implementation
123   config = {
124     # Note: this is set here rather than up there so that changing
125     # nixos.label would not rebuild manual pages
126     services.getty.greetingLine = mkDefault ''<<< Welcome to ${config.system.nixos.distroName} ${config.system.nixos.label} (\m) - \l >>>'';
127     services.getty.helpLine = mkIf (config.documentation.nixos.enable && config.documentation.doc.enable) "\nRun 'nixos-help' for the NixOS manual.";
129     systemd.services."getty@" =
130       { serviceConfig.ExecStart = [
131           # override upstream default with an empty ExecStart
132           ""
133           (pkgs.writers.writeDash "getty" autologinScript)
134         ];
135         environment.TTY = "%I";
136         restartIfChanged = false;
137       };
139     systemd.services."serial-getty@" =
140       { serviceConfig.ExecStart = [
141           "" # override upstream default with an empty ExecStart
142           (gettyCmd "%I --keep-baud $TERM")
143         ];
144         restartIfChanged = false;
145       };
147     systemd.services."autovt@" =
148       { serviceConfig.ExecStart = [
149           "" # override upstream default with an empty ExecStart
150           (gettyCmd "--noclear %I $TERM")
151         ];
152         restartIfChanged = false;
153       };
155     systemd.services."container-getty@" =
156       { serviceConfig.ExecStart = [
157           "" # override upstream default with an empty ExecStart
158           (gettyCmd "--noclear --keep-baud pts/%I 115200,38400,9600 $TERM")
159         ];
160         restartIfChanged = false;
161       };
163     systemd.services.console-getty =
164       { serviceConfig.ExecStart = [
165           "" # override upstream default with an empty ExecStart
166           (gettyCmd "--noclear --keep-baud console 115200,38400,9600 $TERM")
167         ];
168         serviceConfig.Restart = "always";
169         restartIfChanged = false;
170         enable = mkDefault config.boot.isContainer;
171       };
173     environment.etc.issue = mkDefault
174       { # Friendly greeting on the virtual consoles.
175         source = pkgs.writeText "issue" ''
177           \e[1;32m${config.services.getty.greetingLine}\e[0m
178           ${config.services.getty.helpLine}
180         '';
181       };
183   };
185   meta.maintainers = with maintainers; [ RossComputerGuy ];