Skip to content

Commit

Permalink
treewide/nixos: remove with lib; part 4 (#335631)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 committed Sep 15, 2024
2 parents be7d7ef + b4b8ef5 commit 6bb59d8
Show file tree
Hide file tree
Showing 147 changed files with 3,411 additions and 3,832 deletions.
8 changes: 2 additions & 6 deletions nixos/modules/installer/cd-dvd/installation-cd-base.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# This module contains the basic configuration for building a NixOS
# installation CD.

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

with lib;

{
imports =
[ ./iso-image.nix
Expand Down Expand Up @@ -32,8 +28,8 @@ with lib;

# An installation media cannot tolerate a host config defined file
# system layout on a fresh machine, before it has been formatted.
swapDevices = mkImageMediaOverride [ ];
fileSystems = mkImageMediaOverride config.lib.isoFileSystems;
swapDevices = lib.mkImageMediaOverride [ ];
fileSystems = lib.mkImageMediaOverride config.lib.isoFileSystems;

boot.postBootCommands = ''
for o in $(</proc/cmdline); do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# This module contains the basic configuration for building a graphical NixOS
# installation CD.

{ lib, pkgs, ... }:

with lib;

{
imports = [ ./installation-cd-base.nix ];

Expand All @@ -26,7 +22,7 @@ with lib;

# Provide networkmanager for easy wireless configuration.
networking.networkmanager.enable = true;
networking.wireless.enable = mkImageMediaOverride false;
networking.wireless.enable = lib.mkImageMediaOverride false;

# KDE complains if power management is disabled (to be precise, if
# there is no power management backend such as upower).
Expand Down
5 changes: 1 addition & 4 deletions nixos/modules/installer/scan/detected.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# List all devices which are detected by nixos-generate-config.
# Common devices are enabled by default.
{ lib, ... }:

with lib;

{
config = mkDefault {
config = lib.mkDefault {
# Common firmware, i.e. for wifi cards
hardware.enableRedistributableFirmware = true;
};
Expand Down
27 changes: 12 additions & 15 deletions nixos/modules/misc/nixpkgs-flake.nix
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
{ config, options, lib, pkgs, ... }:

with lib;

let
cfg = config.nixpkgs.flake;
in
{
options.nixpkgs.flake = {
source = mkOption {
source = lib.mkOption {
# In newer Nix versions, particularly with lazy trees, outPath of
# flakes becomes a Nix-language path object. We deliberately allow this
# to gracefully come through the interface in discussion with @roberth.
#
# See: https://github.com/NixOS/nixpkgs/pull/278522#discussion_r1460292639
type = types.nullOr (types.either types.str types.path);
type = lib.types.nullOr (lib.types.either lib.types.str lib.types.path);

default = null;
defaultText = "if (using nixpkgsFlake.lib.nixosSystem) then self.outPath else null";
Expand All @@ -34,8 +31,8 @@ in
'';
};

setNixPath = mkOption {
type = types.bool;
setNixPath = lib.mkOption {
type = lib.types.bool;

default = cfg.source != null;
defaultText = "config.nixpkgs.flake.source != null";
Expand All @@ -54,8 +51,8 @@ in
'';
};

setFlakeRegistry = mkOption {
type = types.bool;
setFlakeRegistry = lib.mkOption {
type = lib.types.bool;

default = cfg.source != null;
defaultText = "config.nixpkgs.flake.source != null";
Expand All @@ -75,7 +72,7 @@ in
};
};

config = mkIf (cfg.source != null) (mkMerge [
config = lib.mkIf (cfg.source != null) (lib.mkMerge [
{
assertions = [
{
Expand All @@ -87,19 +84,19 @@ in
}
];
}
(mkIf cfg.setFlakeRegistry {
nix.registry.nixpkgs.to = mkDefault {
(lib.mkIf cfg.setFlakeRegistry {
nix.registry.nixpkgs.to = lib.mkDefault {
type = "path";
path = cfg.source;
};
})
(mkIf cfg.setNixPath {
(lib.mkIf cfg.setNixPath {
# N.B. This does not include nixos-config in NIX_PATH unlike modules/config/nix-channel.nix
# because we would need some kind of evil shim taking the *calling* flake's self path,
# perhaps, to ever make that work (in order to know where the Nix expr for the system came
# from and how to call it).
nix.nixPath = mkDefault ([ "nixpkgs=flake:nixpkgs" ]
++ optional config.nix.channel.enable "/nix/var/nix/profiles/per-user/root/channels");
nix.nixPath = lib.mkDefault ([ "nixpkgs=flake:nixpkgs" ]
++ lib.optional config.nix.channel.enable "/nix/var/nix/profiles/per-user/root/channels");
})
]);
}
81 changes: 39 additions & 42 deletions nixos/modules/misc/nixpkgs.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
{ config, options, lib, pkgs, ... }:

with lib;

let
cfg = config.nixpkgs;
opt = options.nixpkgs;
Expand All @@ -19,54 +16,54 @@ let
lhs = optCall lhs_ { inherit pkgs; };
rhs = optCall rhs_ { inherit pkgs; };
in
recursiveUpdate lhs rhs //
optionalAttrs (lhs ? packageOverrides) {
lib.recursiveUpdate lhs rhs //
lib.optionalAttrs (lhs ? packageOverrides) {
packageOverrides = pkgs:
optCall lhs.packageOverrides pkgs //
optCall (attrByPath [ "packageOverrides" ] { } rhs) pkgs;
optCall (lib.attrByPath [ "packageOverrides" ] { } rhs) pkgs;
} //
optionalAttrs (lhs ? perlPackageOverrides) {
lib.optionalAttrs (lhs ? perlPackageOverrides) {
perlPackageOverrides = pkgs:
optCall lhs.perlPackageOverrides pkgs //
optCall (attrByPath [ "perlPackageOverrides" ] { } rhs) pkgs;
optCall (lib.attrByPath [ "perlPackageOverrides" ] { } rhs) pkgs;
};

configType = mkOptionType {
configType = lib.mkOptionType {
name = "nixpkgs-config";
description = "nixpkgs config";
check = x:
let traceXIfNot = c:
if c x then true
else lib.traceSeqN 1 x false;
in traceXIfNot isConfig;
merge = args: foldr (def: mergeConfig def.value) {};
merge = args: lib.foldr (def: mergeConfig def.value) {};
};

overlayType = mkOptionType {
overlayType = lib.mkOptionType {
name = "nixpkgs-overlay";
description = "nixpkgs overlay";
check = lib.isFunction;
merge = lib.mergeOneOption;
};

pkgsType = types.pkgs // {
pkgsType = lib.types.pkgs // {
# This type is only used by itself, so let's elaborate the description a bit
# for the purpose of documentation.
description = "An evaluation of Nixpkgs; the top level attribute set of packages";
};

hasBuildPlatform = opt.buildPlatform.highestPrio < (mkOptionDefault {}).priority;
hasBuildPlatform = opt.buildPlatform.highestPrio < (lib.mkOptionDefault {}).priority;
hasHostPlatform = opt.hostPlatform.isDefined;
hasPlatform = hasHostPlatform || hasBuildPlatform;

# Context for messages
hostPlatformLine = optionalString hasHostPlatform "${showOptionWithDefLocs opt.hostPlatform}";
buildPlatformLine = optionalString hasBuildPlatform "${showOptionWithDefLocs opt.buildPlatform}";
hostPlatformLine = lib.optionalString hasHostPlatform "${lib.showOptionWithDefLocs opt.hostPlatform}";
buildPlatformLine = lib.optionalString hasBuildPlatform "${lib.showOptionWithDefLocs opt.buildPlatform}";

legacyOptionsDefined =
optional (opt.localSystem.highestPrio < (mkDefault {}).priority) opt.system
++ optional (opt.localSystem.highestPrio < (mkOptionDefault {}).priority) opt.localSystem
++ optional (opt.crossSystem.highestPrio < (mkOptionDefault {}).priority) opt.crossSystem
lib.optional (opt.localSystem.highestPrio < (lib.mkDefault {}).priority) opt.system
++ lib.optional (opt.localSystem.highestPrio < (lib.mkOptionDefault {}).priority) opt.localSystem
++ lib.optional (opt.crossSystem.highestPrio < (lib.mkOptionDefault {}).priority) opt.crossSystem
;

defaultPkgs =
Expand Down Expand Up @@ -99,19 +96,19 @@ in
imports = [
./assertions.nix
./meta.nix
(mkRemovedOptionModule [ "nixpkgs" "initialSystem" ] "The NixOS options `nesting.clone` and `nesting.children` have been deleted, and replaced with named specialisation. Therefore `nixpgks.initialSystem` has no effect anymore.")
(lib.mkRemovedOptionModule [ "nixpkgs" "initialSystem" ] "The NixOS options `nesting.clone` and `nesting.children` have been deleted, and replaced with named specialisation. Therefore `nixpgks.initialSystem` has no effect lib.anymore.")
];

options.nixpkgs = {

pkgs = mkOption {
defaultText = literalExpression ''
pkgs = lib.mkOption {
defaultText = lib.literalExpression ''
import "''${nixos}/.." {
inherit (cfg) config overlays localSystem crossSystem;
}
'';
type = pkgsType;
example = literalExpression "import <nixpkgs> {}";
example = lib.literalExpression "import <nixpkgs> {}";
description = ''
If set, the pkgs argument to all NixOS modules is the value of
this option, extended with `nixpkgs.overlays`, if
Expand Down Expand Up @@ -145,9 +142,9 @@ in
'';
};

config = mkOption {
config = lib.mkOption {
default = {};
example = literalExpression
example = lib.literalExpression
''
{ allowBroken = true; allowUnfree = true; }
'';
Expand All @@ -160,9 +157,9 @@ in
'';
};

overlays = mkOption {
overlays = lib.mkOption {
default = [];
example = literalExpression
example = lib.literalExpression
''
[
(self: super: {
Expand All @@ -173,7 +170,7 @@ in
})
]
'';
type = types.listOf overlayType;
type = lib.types.listOf overlayType;
description = ''
List of overlays to apply to Nixpkgs.
This option allows modifying the Nixpkgs package set accessed through the `pkgs` module argument.
Expand All @@ -184,13 +181,13 @@ in
'';
};

hostPlatform = mkOption {
type = types.either types.str types.attrs; # TODO utilize lib.systems.parsedPlatform
hostPlatform = lib.mkOption {
type = lib.types.either lib.types.str lib.types.attrs; # TODO utilize lib.systems.parsedPlatform
example = { system = "aarch64-linux"; };
# Make sure that the final value has all fields for sake of other modules
# referring to this. TODO make `lib.systems` itself use the module system.
apply = lib.systems.elaborate;
defaultText = literalExpression
defaultText = lib.literalExpression
''(import "''${nixos}/../lib").lib.systems.examples.aarch64-multiplatform'';
description = ''
Specifies the platform where the NixOS configuration will run.
Expand All @@ -201,8 +198,8 @@ in
'';
};

buildPlatform = mkOption {
type = types.either types.str types.attrs; # TODO utilize lib.systems.parsedPlatform
buildPlatform = lib.mkOption {
type = lib.types.either lib.types.str lib.types.attrs; # TODO utilize lib.systems.parsedPlatform
default = cfg.hostPlatform;
example = { system = "x86_64-linux"; };
# Make sure that the final value has all fields for sake of other modules
Expand All @@ -212,7 +209,7 @@ in
in if lib.systems.equals elaborated cfg.hostPlatform
then cfg.hostPlatform # make identical, so that `==` equality works; see https://github.com/NixOS/nixpkgs/issues/278001
else elaborated;
defaultText = literalExpression
defaultText = lib.literalExpression
''config.nixpkgs.hostPlatform'';
description = ''
Specifies the platform on which NixOS should be built.
Expand All @@ -228,14 +225,14 @@ in
'';
};

localSystem = mkOption {
type = types.attrs; # TODO utilize lib.systems.parsedPlatform
localSystem = lib.mkOption {
type = lib.types.attrs; # TODO utilize lib.systems.parsedPlatform
default = { inherit (cfg) system; };
example = { system = "aarch64-linux"; };
# Make sure that the final value has all fields for sake of other modules
# referring to this. TODO make `lib.systems` itself use the module system.
apply = lib.systems.elaborate;
defaultText = literalExpression
defaultText = lib.literalExpression
''(import "''${nixos}/../lib").lib.systems.examples.aarch64-multiplatform'';
description = ''
Systems with a recently generated `hardware-configuration.nix`
Expand All @@ -262,8 +259,8 @@ in
# TODO deprecate. "crossSystem" is a nonsense identifier, because "cross"
# is a relation between at least 2 systems in the context of a
# specific build step, not a single system.
crossSystem = mkOption {
type = types.nullOr types.attrs; # TODO utilize lib.systems.parsedPlatform
crossSystem = lib.mkOption {
type = lib.types.nullOr lib.types.attrs; # TODO utilize lib.systems.parsedPlatform
default = null;
example = { system = "aarch64-linux"; };
description = ''
Expand All @@ -283,8 +280,8 @@ in
'';
};

system = mkOption {
type = types.str;
system = lib.mkOption {
type = lib.types.str;
example = "i686-linux";
default =
if opt.hostPlatform.isDefined
Expand Down Expand Up @@ -372,12 +369,12 @@ in
{
assertion = constructedByMe -> hasPlatform -> legacyOptionsDefined == [];
message = ''
Your system configures nixpkgs with the platform parameter${optionalString hasBuildPlatform "s"}:
Your system configures nixpkgs with the platform parameter${lib.optionalString hasBuildPlatform "s"}:
${hostPlatformLine
}${buildPlatformLine
}
However, it also defines the legacy options:
${concatMapStrings showOptionWithDefLocs legacyOptionsDefined}
${lib.concatMapStrings lib.showOptionWithDefLocs legacyOptionsDefined}
For a future proof system configuration, we recommend to remove
the legacy definitions.
'';
Expand Down
Loading

0 comments on commit 6bb59d8

Please sign in to comment.