1{ config, pkgs, lib, ... }:
2let
3 inherit (lib) mkOption types mkIf optionalString;
4
5 cfg = config.services.kmscon;
6
7 configDir = pkgs.writeTextFile { name = "kmscon-config"; destination = "/kmscon.conf"; text = cfg.extraConfig; };
8in {
9 options = {
10 services.kmscon = {
11 enable = mkOption {
12 description = ''
13 Use kmscon as the virtual console instead of gettys.
14 kmscon is a kms/dri-based userspace virtual terminal implementation.
15 It supports a richer feature set than the standard linux console VT,
16 including full unicode support, and when the video card supports drm
17 should be much faster.
18 '';
19 type = types.bool;
20 default = false;
21 };
22
23 hwRender = mkOption {
24 description = "Whether to use 3D hardware acceleration to render the console.";
25 type = types.bool;
26 default = false;
27 };
28
29 extraConfig = mkOption {
30 description = "Extra contents of the kmscon.conf file.";
31 type = types.lines;
32 default = "";
33 example = "font-size=14";
34 };
35 };
36 };
37
38 config = mkIf cfg.enable {
39 # Largely copied from unit provided with kmscon source
40 systemd.units."kmsconvt@.service".text = ''
41 [Unit]
42 Description=KMS System Console on %I
43 Documentation=man:kmscon(1)
44 After=systemd-user-sessions.service
45 After=plymouth-quit-wait.service
46 After=systemd-logind.service
47 After=systemd-vconsole-setup.service
48 Requires=systemd-logind.service
49 Before=getty.target
50 Conflicts=getty@%i.service
51 OnFailure=getty@%i.service
52 IgnoreOnIsolate=yes
53 ConditionPathExists=/dev/tty0
54
55 [Service]
56 ExecStart=${pkgs.kmscon}/bin/kmscon "--vt=%I" --seats=seat0 --no-switchvt --configdir ${configDir} --login -- ${pkgs.shadow}/bin/login -p
57 UtmpIdentifier=%I
58 TTYPath=/dev/%I
59 TTYReset=yes
60 TTYVHangup=yes
61 TTYVTDisallocate=yes
62
63 X-RestartIfChanged=false
64 '';
65
66 systemd.units."autovt@.service".unit = pkgs.runCommand "unit" { }
67 ''
68 mkdir -p $out
69 ln -s ${config.systemd.units."kmsconvt@.service".unit}/kmsconvt@.service $out/autovt@.service
70 '';
71
72 systemd.services.systemd-vconsole-setup.restartIfChanged = false;
73
74 services.kmscon.extraConfig = mkIf cfg.hwRender ''
75 drm
76 hwaccel
77 '';
78
79 hardware.opengl.enable = mkIf cfg.hwRender true;
80 };
81}