Skip to content

Instantly share code, notes, and snippets.

@caadar
Last active July 29, 2024 04:26
Show Gist options
  • Save caadar/7884b1bf16cb1fc2c7cde33d329ae37f to your computer and use it in GitHub Desktop.
Save caadar/7884b1bf16cb1fc2c7cde33d329ae37f to your computer and use it in GitHub Desktop.
How to make a user autologin at the console on NixOS

Autologin a user at the console (NixOS)

Problem

NixOS provide services.mingetty.autologinUser option, but it can’t be used for the specific TTY.

Minimal working example

TTY1 and user “guest”:

systemd.services."autovt@tty1".description = "Autologin at the TTY1";
systemd.services."autovt@tty1".after = [ "systemd-logind.service" ];  # without it user session not started and xorg can't be run from this tty
systemd.services."autovt@tty1".wantedBy = [ "multi-user.target" ];
systemd.services."autovt@tty1".serviceConfig =
  { ExecStart = [
      ""  # override upstream default with an empty ExecStart
      "@${pkgs.utillinux}/sbin/agetty agetty --login-program ${pkgs.shadow}/bin/login --autologin guest --noclear %I $TERM"
    ];
    Restart = "always";
    Type = "idle";
  };

Complete solution

TTY1 and user “guest”:

systemd.targets."autologin-tty1".description = "Final";
systemd.targets."autologin-tty1".requires = [ "multi-user.target" ];
systemd.targets."autologin-tty1".after = [ "multi-user.target" ];
systemd.targets."autologin-tty1".unitConfig = { AllowIsolate="yes"; };

systemd.services."supress-kernel-logging".description = "Suppress kernel logging to the console";
systemd.services."supress-kernel-logging".after = [ "multi-user.target" ];
systemd.services."supress-kernel-logging".wantedBy = [ "autologin-tty1.target" ];
systemd.services."supress-kernel-logging".serviceConfig =
  { ExecStart = [
      "${pkgs.utillinux}/sbin/dmesg -n 1"
    ];
    Type = "oneshot";
  };

systemd.services."autovt@tty1".description = "Autologin on TTY1";
systemd.services."autovt@tty1".after = [ "supress-kernel-logging.service" ];
systemd.services."autovt@tty1".wantedBy = [ "autologin-tty1.target" ];
systemd.services."autovt@tty1".restartIfChanged = false;
systemd.services."autovt@tty1".serviceConfig =
  { ExecStart = [
      ""  # override upstream default with an empty ExecStart
      "@${pkgs.utillinux}/sbin/agetty agetty --login-program ${pkgs.shadow}/bin/login --autologin guest --noclear %I $TERM"
    ];
    Restart = "always";
    Type = "idle";
  };

Some useful links

https://github.com/a-irs/infra/blob/f73bf14e654df1e0ad9b4a5c1e5c72d0649ac5d6/nixos/etc/conf.d/workstation.nix#L12 https://github.com/coreyoconnor/nix_configs/blob/9aeb6f0ad4a707f0d3f89177774a532c880d3b6a/status-tty.nix#L23 https://web.archive.org/web/20160325165924/https://www.mauras.ch/systemd-run-it-last.html

@CyberShadow
Copy link

CyberShadow commented Jun 26, 2024

This didn't work for me because NixOS creates autovt@.service.d/overrides.conf, which has ExecStart, which overrides the one in the "autovt@tty1" unit created here.

The following worked for me instead:

    # Optional - bump number of VTs from 6 to 7 (if you want to e.g. use startx on VT 7)
    services.logind.extraConfig = ''
      NAutoVTs=7
    '';
    # https://gist.github.com/caadar/7884b1bf16cb1fc2c7cde33d329ae37f
    # https://github.com/NixOS/nixpkgs/issues/81552
    systemd.services."getty@tty7" = {
      overrideStrategy = "asDropin";
      serviceConfig = {
        # Optional - switch to TTY7 right away
        ExecStartPre = [
          "${pkgs.kbd}/bin/chvt 7"
        ];
        ExecStart = [
          ""  # override upstream default with an empty ExecStart
          "@${pkgs.utillinux}/sbin/agetty agetty --login-program ${pkgs.shadow}/bin/login --autologin guest --noclear %I $TERM"
        ];
      };
    };
    # Optional - start getty on TTY7 right away
    systemd.targets."getty".wants = [ "getty@tty7.service" ];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment