2019-10-20 20:55:12 +00:00
|
|
|
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
cfg = config.console;
|
|
|
|
|
2019-11-22 10:50:46 +00:00
|
|
|
makeColor = i: concatMapStringsSep "," (x: "0x" + substring (2*i) 2 x);
|
2019-10-20 20:55:12 +00:00
|
|
|
|
2022-09-08 21:11:45 +00:00
|
|
|
isUnicode = hasSuffix "UTF-8" (toUpper config.i18n.defaultLocale);
|
2019-10-20 20:55:12 +00:00
|
|
|
|
|
|
|
optimizedKeymap = pkgs.runCommand "keymap" {
|
2022-09-08 21:11:45 +00:00
|
|
|
nativeBuildInputs = [ pkgs.buildPackages.kbd ];
|
2022-04-14 08:37:17 +00:00
|
|
|
LOADKEYS_KEYMAP_PATH = "${consoleEnv pkgs.kbd}/share/keymaps/**";
|
2019-10-20 20:55:12 +00:00
|
|
|
preferLocalBuild = true;
|
|
|
|
} ''
|
2022-09-08 21:11:45 +00:00
|
|
|
loadkeys -b ${optionalString isUnicode "-u"} "${cfg.keyMap}" > $out
|
2019-10-20 20:55:12 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
# Sadly, systemd-vconsole-setup doesn't support binary keymaps.
|
|
|
|
vconsoleConf = pkgs.writeText "vconsole.conf" ''
|
|
|
|
KEYMAP=${cfg.keyMap}
|
|
|
|
FONT=${cfg.font}
|
|
|
|
'';
|
|
|
|
|
2022-04-14 08:37:17 +00:00
|
|
|
consoleEnv = kbd: pkgs.buildEnv {
|
2019-10-20 20:55:12 +00:00
|
|
|
name = "console-env";
|
2022-04-14 08:37:17 +00:00
|
|
|
paths = [ kbd ] ++ cfg.packages;
|
2019-10-20 20:55:12 +00:00
|
|
|
pathsToLink = [
|
|
|
|
"/share/consolefonts"
|
|
|
|
"/share/consoletrans"
|
|
|
|
"/share/keymaps"
|
|
|
|
"/share/unimaps"
|
|
|
|
];
|
|
|
|
};
|
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
###### interface
|
|
|
|
|
|
|
|
options.console = {
|
2022-10-26 08:52:50 +00:00
|
|
|
enable = mkEnableOption (lib.mdDoc "virtual console") // {
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
|
2019-10-20 20:55:12 +00:00
|
|
|
font = mkOption {
|
2021-06-12 10:34:04 +00:00
|
|
|
type = with types; either str path;
|
2019-10-20 20:55:12 +00:00
|
|
|
default = "Lat2-Terminus16";
|
|
|
|
example = "LatArCyrHeb-16";
|
2022-06-03 19:47:57 +00:00
|
|
|
description = mdDoc ''
|
2019-10-20 20:55:12 +00:00
|
|
|
The font used for the virtual consoles. Leave empty to use
|
2022-06-03 19:47:57 +00:00
|
|
|
whatever the {command}`setfont` program considers the
|
2019-10-20 20:55:12 +00:00
|
|
|
default font.
|
2021-06-12 10:34:04 +00:00
|
|
|
Can be either a font name or a path to a PSF font file.
|
2019-10-20 20:55:12 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
keyMap = mkOption {
|
|
|
|
type = with types; either str path;
|
|
|
|
default = "us";
|
|
|
|
example = "fr";
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-20 20:55:12 +00:00
|
|
|
The keyboard mapping table for the virtual consoles.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
colors = mkOption {
|
2022-09-04 17:29:49 +00:00
|
|
|
type = with types; listOf (strMatching "[[:xdigit:]]{6}");
|
|
|
|
default = [ ];
|
2019-10-20 20:55:12 +00:00
|
|
|
example = [
|
|
|
|
"002b36" "dc322f" "859900" "b58900"
|
|
|
|
"268bd2" "d33682" "2aa198" "eee8d5"
|
|
|
|
"002b36" "cb4b16" "586e75" "657b83"
|
|
|
|
"839496" "6c71c4" "93a1a1" "fdf6e3"
|
|
|
|
];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-20 20:55:12 +00:00
|
|
|
The 16 colors palette used by the virtual consoles.
|
|
|
|
Leave empty to use the default colors.
|
|
|
|
Colors must be in hexadecimal format and listed in
|
|
|
|
order from color 0 to color 15.
|
|
|
|
'';
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
packages = mkOption {
|
|
|
|
type = types.listOf types.package;
|
2021-04-11 00:09:07 +00:00
|
|
|
default = [ ];
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-20 20:55:12 +00:00
|
|
|
List of additional packages that provide console fonts, keymaps and
|
|
|
|
other resources for virtual consoles use.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
useXkbConfig = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-20 20:55:12 +00:00
|
|
|
If set, configure the virtual console keymap from the xserver
|
|
|
|
keyboard settings.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
earlySetup = mkOption {
|
|
|
|
default = false;
|
|
|
|
type = types.bool;
|
2022-07-28 21:19:15 +00:00
|
|
|
description = lib.mdDoc ''
|
2019-10-20 20:55:12 +00:00
|
|
|
Enable setting virtual console options as early as possible (in initrd).
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
###### implementation
|
|
|
|
|
|
|
|
config = mkMerge [
|
|
|
|
{ console.keyMap = with config.services.xserver;
|
|
|
|
mkIf cfg.useXkbConfig
|
|
|
|
(pkgs.runCommand "xkb-console-keymap" { preferLocalBuild = true; } ''
|
2021-10-20 00:15:09 +00:00
|
|
|
'${pkgs.buildPackages.ckbcomp}/bin/ckbcomp' \
|
2021-09-16 23:39:38 +00:00
|
|
|
${optionalString (config.environment.sessionVariables ? XKB_CONFIG_ROOT)
|
|
|
|
"-I${config.environment.sessionVariables.XKB_CONFIG_ROOT}"
|
|
|
|
} \
|
|
|
|
-model '${xkbModel}' -layout '${layout}' \
|
2019-10-20 20:55:12 +00:00
|
|
|
-option '${xkbOptions}' -variant '${xkbVariant}' > "$out"
|
|
|
|
'');
|
|
|
|
}
|
|
|
|
|
2022-10-26 08:52:50 +00:00
|
|
|
(mkIf (!cfg.enable) {
|
|
|
|
systemd.services = {
|
|
|
|
"serial-getty@ttyS0".enable = false;
|
|
|
|
"serial-getty@hvc0".enable = false;
|
|
|
|
"getty@tty1".enable = false;
|
|
|
|
"autovt@".enable = false;
|
|
|
|
systemd-vconsole-setup.enable = false;
|
|
|
|
};
|
2019-10-20 20:55:12 +00:00
|
|
|
})
|
|
|
|
|
2022-10-26 08:52:50 +00:00
|
|
|
(mkIf cfg.enable (mkMerge [
|
2022-09-08 21:11:45 +00:00
|
|
|
{ environment.systemPackages = [ pkgs.kbd ];
|
2019-10-20 20:55:12 +00:00
|
|
|
|
|
|
|
# Let systemd-vconsole-setup.service do the work of setting up the
|
|
|
|
# virtual consoles.
|
|
|
|
environment.etc."vconsole.conf".source = vconsoleConf;
|
|
|
|
# Provide kbd with additional packages.
|
2022-04-14 08:37:17 +00:00
|
|
|
environment.etc.kbd.source = "${consoleEnv pkgs.kbd}/share";
|
2019-10-20 20:55:12 +00:00
|
|
|
|
2022-04-14 08:37:17 +00:00
|
|
|
boot.initrd.preLVMCommands = mkIf (!config.boot.initrd.systemd.enable) (mkBefore ''
|
2022-09-08 21:11:45 +00:00
|
|
|
kbd_mode ${if isUnicode then "-u" else "-a"} -C /dev/console
|
|
|
|
printf "\033%%${if isUnicode then "G" else "@"}" >> /dev/console
|
2019-10-20 20:55:12 +00:00
|
|
|
loadkmap < ${optimizedKeymap}
|
|
|
|
|
|
|
|
${optionalString cfg.earlySetup ''
|
|
|
|
setfont -C /dev/console $extraUtils/share/consolefonts/font.psf
|
|
|
|
''}
|
2022-04-14 08:37:17 +00:00
|
|
|
'');
|
|
|
|
|
|
|
|
boot.initrd.systemd.contents = {
|
|
|
|
"/etc/vconsole.conf".source = vconsoleConf;
|
2022-04-24 20:38:16 +00:00
|
|
|
# Add everything if we want full console setup...
|
|
|
|
"/etc/kbd" = lib.mkIf cfg.earlySetup { source = "${consoleEnv config.boot.initrd.systemd.package.kbd}/share"; };
|
|
|
|
# ...but only the keymaps if we don't
|
|
|
|
"/etc/kbd/keymaps" = lib.mkIf (!cfg.earlySetup) { source = "${consoleEnv config.boot.initrd.systemd.package.kbd}/share/keymaps"; };
|
2022-04-14 08:37:17 +00:00
|
|
|
};
|
|
|
|
boot.initrd.systemd.storePaths = [
|
2022-04-14 21:07:41 +00:00
|
|
|
"${config.boot.initrd.systemd.package}/lib/systemd/systemd-vconsole-setup"
|
2022-04-14 08:37:17 +00:00
|
|
|
"${config.boot.initrd.systemd.package.kbd}/bin/setfont"
|
|
|
|
"${config.boot.initrd.systemd.package.kbd}/bin/loadkeys"
|
2022-06-22 03:35:11 +00:00
|
|
|
"${config.boot.initrd.systemd.package.kbd.gzip}/bin/gzip" # Fonts and keyboard layouts are compressed
|
|
|
|
] ++ optionals (hasPrefix builtins.storeDir cfg.font) [
|
|
|
|
"${cfg.font}"
|
|
|
|
] ++ optionals (hasPrefix builtins.storeDir cfg.keyMap) [
|
|
|
|
"${cfg.keyMap}"
|
2022-04-14 08:37:17 +00:00
|
|
|
];
|
2019-10-20 20:55:12 +00:00
|
|
|
|
nixos/console: fix console setting reloading
It's a dull and boring day, it's cold outside and I'm stuck at home: let
me tell you the story of systemd-vconsole-setup.
In the beginnings of NixOS[1], systemd-vconsole-setup was a powerful
sysinit.target unit, installed and running at boot to set up fonts
keyboard layouts and even colors of the virtual consoles. If needed, the
service would also be restarted after a configuration change, consoles
were happy and everything was good, well, almost.
Since the service had no way to specify the dependency "ttys are ready",
modesetting could sometimes happen *after* systemd-vconsole-setup had
started, leaving the console in a broken state. So abbradar worked
around that by putting a systemd-udev-settle `After=`.
In the meanwhile, probably realizing their mistake, systemd added a
shiny udev rule to start the systemd-udev-settle at the right time[2].
However, the rule bypassed systemd by directly running the binary
`systemd-udev-settle`, and the service - though still installed - fell
into disuse.
Two years would pass before a good samaritan, seeing the poor jobless
systemd-udev-settle service, decided to give it the coup de grâs[3] by
unlisting it from the installed units.
This, combined with another bug, caused quite a commotion[4] in NixOS;
to see why remember the fact that `WantedBy=` in upstream units doesn't
work[5], so it had to be added manually in cc542110, but while systemd
removed it, the NixOS unit continued to install and restart the service,
making a lot of fuss when switching configuration.
After at least thee different tentative fixes, deedrah realised[6] what
the root cause was and fpletz put the final nail[7] in the coffin of
systemd-udev-settle. The service would never see the light of a boot
again, NixOS would not restart it all the time but thanks to udev
consoles would still get their pretty fonts and playful colors.
The En..
..no, wait! You should ask what came of systemd-udev-settle, first.
And why is the service even around if udev is doing all the work?
Udev-settle, like the deceitful snake that he is, laid hidden for years.
He looks innocuous doesn't it? A little hack. Only until it leaves his
den and a poor user[8] drops dead. Obviously, it serves no purpose, as
the service is not part of the boot process anymore, so let's remove it
for good!
About the service, it may not be useful at boot, but it can be started
to pick up changes in vconsole.conf and set the consoles accordingly.
But wait, this doesn't work anymore: the service is never started at
boot (remember f76d2aa6), so switch-to-configuration.pl will not restart
it. Fortunately it can be repaired: here I install a new unit which
does *nothing* on start, but restarts the real service when reloaded.
This perfectly reproduces the original behavior, hopefully without the
original bugs too.
The End?
[1]: https://github.com/NixOS/nixpkgs/commit/cc5421106942ce82473f4afd74d156d8d1e303bd
[2]: https://github.com/systemd/systemd/commit/f6ba8671d83f9fce9a00045d8fa399a1c07ba7fc#diff-84849fddcef81458f69725dc18c6614aade5c4f41a032b6908ebcf1ee6740636
[3]: https://github.com/systemd/systemd/commit/8125e8d38e3aa099c7dce8b0161997b8842aebdc
[4]: https://web.archive.org/web/20180603130107/https://github.com/NixOS/nixpkgs/issues/22470
[5]: https://github.com/NixOS/nixpkgs/issues/81138
[6]: https://web.archive.org/web/20180603130107/https://github.com/NixOS/nixpkgs/issues/22470#issuecomment-330930456
[7]: https://github.com/NixOS/nixpkgs/commit/f76d2aa6e3b4ed6ca9da1e761b1ef2ec36b227c8
[8]: https://github.com/NixOS/nixpkgs/issues/107341
2021-02-20 15:07:38 +00:00
|
|
|
systemd.services.reload-systemd-vconsole-setup =
|
|
|
|
{ description = "Reset console on configuration changes";
|
|
|
|
wantedBy = [ "multi-user.target" ];
|
2022-04-14 08:37:17 +00:00
|
|
|
restartTriggers = [ vconsoleConf (consoleEnv pkgs.kbd) ];
|
nixos/console: fix console setting reloading
It's a dull and boring day, it's cold outside and I'm stuck at home: let
me tell you the story of systemd-vconsole-setup.
In the beginnings of NixOS[1], systemd-vconsole-setup was a powerful
sysinit.target unit, installed and running at boot to set up fonts
keyboard layouts and even colors of the virtual consoles. If needed, the
service would also be restarted after a configuration change, consoles
were happy and everything was good, well, almost.
Since the service had no way to specify the dependency "ttys are ready",
modesetting could sometimes happen *after* systemd-vconsole-setup had
started, leaving the console in a broken state. So abbradar worked
around that by putting a systemd-udev-settle `After=`.
In the meanwhile, probably realizing their mistake, systemd added a
shiny udev rule to start the systemd-udev-settle at the right time[2].
However, the rule bypassed systemd by directly running the binary
`systemd-udev-settle`, and the service - though still installed - fell
into disuse.
Two years would pass before a good samaritan, seeing the poor jobless
systemd-udev-settle service, decided to give it the coup de grâs[3] by
unlisting it from the installed units.
This, combined with another bug, caused quite a commotion[4] in NixOS;
to see why remember the fact that `WantedBy=` in upstream units doesn't
work[5], so it had to be added manually in cc542110, but while systemd
removed it, the NixOS unit continued to install and restart the service,
making a lot of fuss when switching configuration.
After at least thee different tentative fixes, deedrah realised[6] what
the root cause was and fpletz put the final nail[7] in the coffin of
systemd-udev-settle. The service would never see the light of a boot
again, NixOS would not restart it all the time but thanks to udev
consoles would still get their pretty fonts and playful colors.
The En..
..no, wait! You should ask what came of systemd-udev-settle, first.
And why is the service even around if udev is doing all the work?
Udev-settle, like the deceitful snake that he is, laid hidden for years.
He looks innocuous doesn't it? A little hack. Only until it leaves his
den and a poor user[8] drops dead. Obviously, it serves no purpose, as
the service is not part of the boot process anymore, so let's remove it
for good!
About the service, it may not be useful at boot, but it can be started
to pick up changes in vconsole.conf and set the consoles accordingly.
But wait, this doesn't work anymore: the service is never started at
boot (remember f76d2aa6), so switch-to-configuration.pl will not restart
it. Fortunately it can be repaired: here I install a new unit which
does *nothing* on start, but restarts the real service when reloaded.
This perfectly reproduces the original behavior, hopefully without the
original bugs too.
The End?
[1]: https://github.com/NixOS/nixpkgs/commit/cc5421106942ce82473f4afd74d156d8d1e303bd
[2]: https://github.com/systemd/systemd/commit/f6ba8671d83f9fce9a00045d8fa399a1c07ba7fc#diff-84849fddcef81458f69725dc18c6614aade5c4f41a032b6908ebcf1ee6740636
[3]: https://github.com/systemd/systemd/commit/8125e8d38e3aa099c7dce8b0161997b8842aebdc
[4]: https://web.archive.org/web/20180603130107/https://github.com/NixOS/nixpkgs/issues/22470
[5]: https://github.com/NixOS/nixpkgs/issues/81138
[6]: https://web.archive.org/web/20180603130107/https://github.com/NixOS/nixpkgs/issues/22470#issuecomment-330930456
[7]: https://github.com/NixOS/nixpkgs/commit/f76d2aa6e3b4ed6ca9da1e761b1ef2ec36b227c8
[8]: https://github.com/NixOS/nixpkgs/issues/107341
2021-02-20 15:07:38 +00:00
|
|
|
reloadIfChanged = true;
|
|
|
|
serviceConfig =
|
|
|
|
{ RemainAfterExit = true;
|
|
|
|
ExecStart = "${pkgs.coreutils}/bin/true";
|
|
|
|
ExecReload = "/run/current-system/systemd/bin/systemctl restart systemd-vconsole-setup";
|
|
|
|
};
|
2019-10-20 20:55:12 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-22 10:50:46 +00:00
|
|
|
(mkIf (cfg.colors != []) {
|
|
|
|
boot.kernelParams = [
|
|
|
|
"vt.default_red=${makeColor 0 cfg.colors}"
|
|
|
|
"vt.default_grn=${makeColor 1 cfg.colors}"
|
|
|
|
"vt.default_blu=${makeColor 2 cfg.colors}"
|
|
|
|
];
|
|
|
|
})
|
|
|
|
|
2022-04-30 21:43:09 +00:00
|
|
|
(mkIf (cfg.earlySetup && !config.boot.initrd.systemd.enable) {
|
2019-10-20 20:55:12 +00:00
|
|
|
boot.initrd.extraUtilsCommands = ''
|
|
|
|
mkdir -p $out/share/consolefonts
|
|
|
|
${if substring 0 1 cfg.font == "/" then ''
|
|
|
|
font="${cfg.font}"
|
|
|
|
'' else ''
|
2022-04-14 08:37:17 +00:00
|
|
|
font="$(echo ${consoleEnv pkgs.kbd}/share/consolefonts/${cfg.font}.*)"
|
2019-10-20 20:55:12 +00:00
|
|
|
''}
|
|
|
|
if [[ $font == *.gz ]]; then
|
|
|
|
gzip -cd $font > $out/share/consolefonts/font.psf
|
|
|
|
else
|
|
|
|
cp -L $font $out/share/consolefonts/font.psf
|
|
|
|
fi
|
|
|
|
'';
|
|
|
|
})
|
|
|
|
]))
|
|
|
|
];
|
|
|
|
|
|
|
|
imports = [
|
|
|
|
(mkRenamedOptionModule [ "i18n" "consoleFont" ] [ "console" "font" ])
|
|
|
|
(mkRenamedOptionModule [ "i18n" "consoleKeyMap" ] [ "console" "keyMap" ])
|
|
|
|
(mkRenamedOptionModule [ "i18n" "consoleColors" ] [ "console" "colors" ])
|
|
|
|
(mkRenamedOptionModule [ "i18n" "consolePackages" ] [ "console" "packages" ])
|
|
|
|
(mkRenamedOptionModule [ "i18n" "consoleUseXkbConfig" ] [ "console" "useXkbConfig" ])
|
|
|
|
(mkRenamedOptionModule [ "boot" "earlyVconsoleSetup" ] [ "console" "earlySetup" ])
|
|
|
|
(mkRenamedOptionModule [ "boot" "extraTTYs" ] [ "console" "extraTTYs" ])
|
2020-12-17 20:22:38 +00:00
|
|
|
(mkRemovedOptionModule [ "console" "extraTTYs" ] ''
|
|
|
|
Since NixOS switched to systemd (circa 2012), TTYs have been spawned on
|
|
|
|
demand, so there is no need to configure them manually.
|
|
|
|
'')
|
2019-10-20 20:55:12 +00:00
|
|
|
];
|
|
|
|
}
|