Skip to content

Commit

Permalink
treewide/nixos: remove with lib; part 1 (#335603)
Browse files Browse the repository at this point in the history
  • Loading branch information
philiptaron authored Aug 29, 2024
2 parents dc8a003 + 14dad27 commit 117f3ce
Show file tree
Hide file tree
Showing 139 changed files with 1,940 additions and 2,343 deletions.
8 changes: 3 additions & 5 deletions nixos/modules/config/appstream.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{ config, lib, ... }:

with lib;
{
options = {
appstream.enable = mkOption {
type = types.bool;
appstream.enable = lib.mkOption {
type = lib.types.bool;
default = true;
description = ''
Whether to install files to support the
Expand All @@ -13,7 +11,7 @@ with lib;
};
};

config = mkIf config.appstream.enable {
config = lib.mkIf config.appstream.enable {
environment.pathsToLink = [
# per component metadata
"/share/metainfo"
Expand Down
78 changes: 37 additions & 41 deletions nixos/modules/config/console.nix
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@

{ config, lib, pkgs, ... }:

with lib;

let
cfg = config.console;

makeColor = i: concatMapStringsSep "," (x: "0x" + substring (2*i) 2 x);
makeColor = i: lib.concatMapStringsSep "," (x: "0x" + lib.substring (2*i) 2 x);

isUnicode = hasSuffix "UTF-8" (toUpper config.i18n.defaultLocale);
isUnicode = lib.hasSuffix "UTF-8" (lib.toUpper config.i18n.defaultLocale);

optimizedKeymap = pkgs.runCommand "keymap" {
nativeBuildInputs = [ pkgs.buildPackages.kbd ];
LOADKEYS_KEYMAP_PATH = "${consoleEnv pkgs.kbd}/share/keymaps/**";
preferLocalBuild = true;
} ''
loadkeys -b ${optionalString isUnicode "-u"} "${cfg.keyMap}" > $out
loadkeys -b ${lib.optionalString isUnicode "-u"} "${cfg.keyMap}" > $out
'';

# Sadly, systemd-vconsole-setup doesn't support binary keymaps.
vconsoleConf = pkgs.writeText "vconsole.conf" ''
KEYMAP=${cfg.keyMap}
${optionalString (cfg.font != null) "FONT=${cfg.font}"}
${lib.optionalString (cfg.font != null) "FONT=${cfg.font}"}
'';

consoleEnv = kbd: pkgs.buildEnv {
Expand All @@ -40,12 +36,12 @@ in
###### interface

options.console = {
enable = mkEnableOption "virtual console" // {
enable = lib.mkEnableOption "virtual console" // {
default = true;
};

font = mkOption {
type = with types; nullOr (either str path);
font = lib.mkOption {
type = with lib.types; nullOr (either str path);
default = null;
example = "LatArCyrHeb-16";
description = ''
Expand All @@ -61,17 +57,17 @@ in
'';
};

keyMap = mkOption {
type = with types; either str path;
keyMap = lib.mkOption {
type = with lib.types; either str path;
default = "us";
example = "fr";
description = ''
The keyboard mapping table for the virtual consoles.
'';
};

colors = mkOption {
type = with types; listOf (strMatching "[[:xdigit:]]{6}");
colors = lib.mkOption {
type = with lib.types; listOf (strMatching "[[:xdigit:]]{6}");
default = [ ];
example = [
"002b36" "dc322f" "859900" "b58900"
Expand All @@ -88,27 +84,27 @@ in

};

packages = mkOption {
type = types.listOf types.package;
packages = lib.mkOption {
type = lib.types.listOf lib.types.package;
default = [ ];
description = ''
List of additional packages that provide console fonts, keymaps and
other resources for virtual consoles use.
'';
};

useXkbConfig = mkOption {
type = types.bool;
useXkbConfig = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
If set, configure the virtual console keymap from the xserver
keyboard settings.
'';
};

earlySetup = mkOption {
earlySetup = lib.mkOption {
default = false;
type = types.bool;
type = lib.types.bool;
description = ''
Enable setting virtual console options as early as possible (in initrd).
'';
Expand All @@ -119,20 +115,20 @@ in

###### implementation

config = mkMerge [
config = lib.mkMerge [
{ console.keyMap = with config.services.xserver;
mkIf cfg.useXkbConfig
lib.mkIf cfg.useXkbConfig
(pkgs.runCommand "xkb-console-keymap" { preferLocalBuild = true; } ''
'${pkgs.buildPackages.ckbcomp}/bin/ckbcomp' \
${optionalString (config.environment.sessionVariables ? XKB_CONFIG_ROOT)
${lib.optionalString (config.environment.sessionVariables ? XKB_CONFIG_ROOT)
"-I${config.environment.sessionVariables.XKB_CONFIG_ROOT}"
} \
-model '${xkb.model}' -layout '${xkb.layout}' \
-option '${xkb.options}' -variant '${xkb.variant}' > "$out"
'');
}

(mkIf (!cfg.enable) {
(lib.mkIf (!cfg.enable) {
systemd.services = {
"serial-getty@ttyS0".enable = false;
"serial-getty@hvc0".enable = false;
Expand All @@ -142,7 +138,7 @@ in
};
})

(mkIf cfg.enable (mkMerge [
(lib.mkIf cfg.enable (lib.mkMerge [
{ environment.systemPackages = [ pkgs.kbd ];

# Let systemd-vconsole-setup.service do the work of setting up the
Expand All @@ -151,12 +147,12 @@ in
# Provide kbd with additional packages.
environment.etc.kbd.source = "${consoleEnv pkgs.kbd}/share";

boot.initrd.preLVMCommands = mkIf (!config.boot.initrd.systemd.enable) (mkBefore ''
boot.initrd.preLVMCommands = lib.mkIf (!config.boot.initrd.systemd.enable) (lib.mkBefore ''
kbd_mode ${if isUnicode then "-u" else "-a"} -C /dev/console
printf "\033%%${if isUnicode then "G" else "@"}" >> /dev/console
loadkmap < ${optimizedKeymap}
${optionalString (cfg.earlySetup && cfg.font != null) ''
${lib.optionalString (cfg.earlySetup && cfg.font != null) ''
setfont -C /dev/console $extraUtils/share/consolefonts/font.psf
''}
'');
Expand All @@ -176,9 +172,9 @@ in
"${config.boot.initrd.systemd.package.kbd}/bin/setfont"
"${config.boot.initrd.systemd.package.kbd}/bin/loadkeys"
"${config.boot.initrd.systemd.package.kbd.gzip}/bin/gzip" # Fonts and keyboard layouts are compressed
] ++ optionals (cfg.font != null && hasPrefix builtins.storeDir cfg.font) [
] ++ lib.optionals (cfg.font != null && lib.hasPrefix builtins.storeDir cfg.font) [
"${cfg.font}"
] ++ optionals (hasPrefix builtins.storeDir cfg.keyMap) [
] ++ lib.optionals (lib.hasPrefix builtins.storeDir cfg.keyMap) [
"${cfg.keyMap}"
];

Expand All @@ -195,18 +191,18 @@ in
};
}

(mkIf (cfg.colors != []) {
(lib.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}"
];
})

(mkIf (cfg.earlySetup && cfg.font != null && !config.boot.initrd.systemd.enable) {
(lib.mkIf (cfg.earlySetup && cfg.font != null && !config.boot.initrd.systemd.enable) {
boot.initrd.extraUtilsCommands = ''
mkdir -p $out/share/consolefonts
${if substring 0 1 cfg.font == "/" then ''
${if lib.substring 0 1 cfg.font == "/" then ''
font="${cfg.font}"
'' else ''
font="$(echo ${consoleEnv pkgs.kbd}/share/consolefonts/${cfg.font}.*)"
Expand All @@ -222,14 +218,14 @@ in
];

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" ])
(mkRemovedOptionModule [ "console" "extraTTYs" ] ''
(lib.mkRenamedOptionModule [ "i18n" "consoleFont" ] [ "console" "font" ])
(lib.mkRenamedOptionModule [ "i18n" "consoleKeyMap" ] [ "console" "keyMap" ])
(lib.mkRenamedOptionModule [ "i18n" "consoleColors" ] [ "console" "colors" ])
(lib.mkRenamedOptionModule [ "i18n" "consolePackages" ] [ "console" "packages" ])
(lib.mkRenamedOptionModule [ "i18n" "consoleUseXkbConfig" ] [ "console" "useXkbConfig" ])
(lib.mkRenamedOptionModule [ "boot" "earlyVconsoleSetup" ] [ "console" "earlySetup" ])
(lib.mkRenamedOptionModule [ "boot" "extraTTYs" ] [ "console" "extraTTYs" ])
(lib.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.
'')
Expand Down
9 changes: 3 additions & 6 deletions nixos/modules/config/debug-info.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
{ config, lib, ... }:

with lib;

{

options = {

environment.enableDebugInfo = mkOption {
type = types.bool;
environment.enableDebugInfo = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Some NixOS packages provide debug symbols. However, these are
Expand All @@ -29,7 +26,7 @@ with lib;
};


config = mkIf config.environment.enableDebugInfo {
config = lib.mkIf config.environment.enableDebugInfo {

# FIXME: currently disabled because /lib is already in
# environment.pathsToLink, and we can't have both.
Expand Down
19 changes: 8 additions & 11 deletions nixos/modules/config/fonts/fontdir.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{ config, lib, pkgs, ... }:

with lib;

let

cfg = config.fonts.fontDir;
Expand All @@ -12,7 +9,7 @@ let
find ${toString config.fonts.packages} -regex "$font_regexp" \
-exec ln -sf -t "$out/share/X11/fonts" '{}' \;
cd "$out/share/X11/fonts"
${optionalString cfg.decompressFonts ''
${lib.optionalString cfg.decompressFonts ''
${pkgs.gzip}/bin/gunzip -f *.gz
''}
${pkgs.xorg.mkfontscale}/bin/mkfontscale
Expand All @@ -27,19 +24,19 @@ in
options = {
fonts.fontDir = {

enable = mkOption {
type = types.bool;
enable = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to create a directory with links to all fonts in
{file}`/run/current-system/sw/share/X11/fonts`.
'';
};

decompressFonts = mkOption {
type = types.bool;
decompressFonts = lib.mkOption {
type = lib.types.bool;
default = config.programs.xwayland.enable;
defaultText = literalExpression "config.programs.xwayland.enable";
defaultText = lib.literalExpression "config.programs.xwayland.enable";
description = ''
Whether to decompress fonts in
{file}`/run/current-system/sw/share/X11/fonts`.
Expand All @@ -49,7 +46,7 @@ in
};
};

config = mkIf cfg.enable {
config = lib.mkIf cfg.enable {

environment.systemPackages = [ x11Fonts ];
environment.pathsToLink = [ "/share/X11/fonts" ];
Expand All @@ -61,7 +58,7 @@ in
};

imports = [
(mkRenamedOptionModule [ "fonts" "enableFontDir" ] [ "fonts" "fontDir" "enable" ])
(lib.mkRenamedOptionModule [ "fonts" "enableFontDir" ] [ "fonts" "fontDir" "enable" ])
];

}
9 changes: 3 additions & 6 deletions nixos/modules/config/fonts/ghostscript.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{ config, lib, pkgs, ... }:

with lib;

{
options = {
fonts.enableGhostscriptFonts = mkOption {
type = types.bool;
fonts.enableGhostscriptFonts = lib.mkOption {
type = lib.types.bool;
default = false;
description = ''
Whether to add the fonts provided by Ghostscript (such as
Expand All @@ -17,7 +14,7 @@ with lib;

};

config = mkIf config.fonts.enableGhostscriptFonts {
config = lib.mkIf config.fonts.enableGhostscriptFonts {
fonts.packages = [ pkgs.ghostscript.fonts ];
};
}
10 changes: 4 additions & 6 deletions nixos/modules/config/gtk/gtk-icon-cache.nix
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
{ config, lib, pkgs, ... }:

with lib;
{
options = {
gtk.iconCache.enable = mkOption {
type = types.bool;
gtk.iconCache.enable = lib.mkOption {
type = lib.types.bool;
default = config.services.xserver.enable;
defaultText = literalExpression "config.services.xserver.enable";
defaultText = lib.literalExpression "config.services.xserver.enable";
description = ''
Whether to build icon theme caches for GTK applications.
'';
};
};

config = mkIf config.gtk.iconCache.enable {
config = lib.mkIf config.gtk.iconCache.enable {

# (Re)build icon theme caches
# ---------------------------
Expand Down
11 changes: 4 additions & 7 deletions nixos/modules/config/iproute2.nix
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
{ config, lib, pkgs, ... }:

with lib;

let
cfg = config.networking.iproute2;
in
{
options.networking.iproute2 = {
enable = mkEnableOption "copying IP route configuration files";
rttablesExtraConfig = mkOption {
type = types.lines;
enable = lib.mkEnableOption "copying IP route configuration files";
rttablesExtraConfig = lib.mkOption {
type = lib.types.lines;
default = "";
description = ''
Verbatim lines to add to /etc/iproute2/rt_tables
'';
};
};

config = mkIf cfg.enable {
config = lib.mkIf cfg.enable {
environment.etc."iproute2/rt_tables.d/nixos.conf" = {
mode = "0644";
text = cfg.rttablesExtraConfig;
Expand Down
Loading

0 comments on commit 117f3ce

Please sign in to comment.