Major overhaul of the nix side of things, part II.

This commit is contained in:
r-vdp 2023-03-24 16:03:20 +01:00
parent 9759c2da12
commit ce4cf7149d
No known key found for this signature in database
7 changed files with 371 additions and 303 deletions

18
flake.lock generated
View file

@ -121,6 +121,23 @@
"type": "github"
}
},
"nixpkgs-nonflake": {
"flake": false,
"locked": {
"lastModified": 1679437018,
"narHash": "sha256-vOuiDPLHSEo/7NkiWtxpHpHgoXoNmrm+wkXZ6a072Fc=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "19cf008bb18e47b6e3b4e16e32a9a4bdd4b45f7e",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-stable": {
"locked": {
"lastModified": 1678872516,
@ -169,6 +186,7 @@
"devshell": "devshell",
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs",
"nixpkgs-nonflake": "nixpkgs-nonflake",
"pre-commit-hooks": "pre-commit-hooks",
"rust-overlay": "rust-overlay",
"treefmt-nix": "treefmt-nix"

View file

@ -1,6 +1,12 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
# We re-use the systemd lib from NixOS, this input allows to import the needed modules.
# TODO: is there a better way to do this?
nixpkgs-nonflake = {
url = "github:NixOS/nixpkgs/nixos-unstable";
flake = false;
};
flake-utils.url = "github:numtide/flake-utils";
devshell = {
url = "github:numtide/devshell";
@ -41,6 +47,7 @@
outputs =
{ self
, nixpkgs
, nixpkgs-nonflake
, flake-utils
, rust-overlay
, crane
@ -49,7 +56,9 @@
, pre-commit-hooks
,
}:
(flake-utils.lib.eachDefaultSystem (system:
(flake-utils.lib.eachSystem
(with flake-utils.lib.system; [ x86_64-linux aarch64-linux ])
(system:
let
pkgs = import nixpkgs {
inherit system;
@ -180,6 +189,7 @@
{
lib = import ./nix/lib.nix {
inherit nixpkgs self;
nixosModules = "${nixpkgs-nonflake}/nixos";
};
systemConfigs.default = self.lib.makeSystemConfig {

View file

@ -1,5 +1,6 @@
{ nixpkgs
, self
{ nixpkgs # The nixpkgs flake
, self # The system-manager flake
, nixosModules # The path to the nixos modules dir from nixpkgs
,
}:
let
@ -16,22 +17,30 @@ in
pkgs = nixpkgs.legacyPackages.${system};
inherit (self.packages.${system}) system-manager;
# TODO can we call lib.evalModules directly instead of building a NixOS system?
nixosConfig = (lib.nixosSystem {
inherit system;
# Module that sets additional module arguments
extraArgsModule = { lib, config, pkgs, ... }: {
_module.args = {
pkgs = nixpkgs.legacyPackages.${system};
utils = import "${nixosModules}/lib/utils.nix" {
inherit lib config pkgs;
};
};
};
config = (lib.evalModules {
modules = [
extraArgsModule
./modules/system-manager.nix
] ++ modules;
specialArgs = extraSpecialArgs;
}).config;
returnIfNoAssertions = drv:
let
failedAssertions = map (x: x.message) (lib.filter (x: !x.assertion) nixosConfig.assertions);
failedAssertions = map (x: x.message) (lib.filter (x: !x.assertion) config.assertions);
in
if failedAssertions != [ ]
then throw "\nFailed assertions:\n${lib.concatStringsSep "\n" (map (x: "- ${x}") failedAssertions)}"
else lib.showWarnings nixosConfig.warnings drv;
else lib.showWarnings config.warnings drv;
services =
lib.mapAttrs'
@ -40,7 +49,7 @@ in
storePath =
''${unit.unit}/${unitName}'';
})
nixosConfig.system-manager.systemd.units;
config.systemd.units;
servicesPath = pkgs.writeTextFile {
name = "services";
@ -64,7 +73,7 @@ in
filteredEntries = lib.filterAttrs
(_name: etcFile: etcFile.enable)
nixosConfig.system-manager.environment.etc;
config.environment.etc;
srcDrvs = lib.mapAttrs addToStore filteredEntries;
@ -131,7 +140,7 @@ in
declare -a failed_assertions=()
${mkAssertions nixosConfig.system-manager.preActivationAssertions}
${mkAssertions config.system-manager.preActivationAssertions}
if [ ''${#failed_assertions[@]} -ne 0 ]; then
for failed_assertion in ''${failed_assertions[@]}; do

View file

@ -4,7 +4,6 @@
}:
{
config = {
system-manager = {
environment.etc = {
foo = {
text = ''
@ -78,5 +77,4 @@
})
);
};
};
}

View file

@ -3,7 +3,7 @@
, ...
}:
{
options.system-manager = {
options = {
environment.etc = lib.mkOption {
default = { };
example = lib.literalExpression ''
@ -100,7 +100,6 @@
Changing this option takes precedence over `gid`.
'';
};
};
config = {
@ -110,7 +109,6 @@
in lib.mkDerivedConfig options.text (pkgs.writeText name')
);
};
}
));
};

View file

@ -8,7 +8,31 @@
./systemd.nix
];
options.system-manager = {
options = {
assertions = lib.mkOption {
type = lib.types.listOf lib.types.unspecified;
internal = true;
default = [ ];
example = [{ assertion = false; message = "you can't enable this for that reason"; }];
description = lib.mdDoc ''
This option allows modules to express conditions that must
hold for the evaluation of the system configuration to
succeed, along with associated error messages for the user.
'';
};
warnings = lib.mkOption {
internal = true;
default = [ ];
type = lib.types.listOf lib.types.str;
example = [ "The `foo' service is deprecated and will go away soon!" ];
description = lib.mdDoc ''
This option allows modules to show warnings to users during
the evaluation of the system configuration.
'';
};
system-manager = {
allowAnyDistro = lib.mkEnableOption "the usage of system-manager on untested distributions";
preActivationAssertions = lib.mkOption {
@ -29,14 +53,9 @@
default = { };
};
};
};
config = {
# Avoid some standard NixOS assertions
boot = {
loader.grub.enable = false;
initrd.enable = false;
};
system.stateVersion = lib.mkDefault lib.trivial.release;
system-manager.preActivationAssertions = {
osVersion =

View file

@ -6,13 +6,31 @@
}:
let
cfg = config.system-manager.systemd;
cfg = config.systemd;
inherit (utils) systemdUtils;
systemd-lib = utils.systemdUtils.lib;
in
{
options.system-manager.systemd = {
options.systemd = {
# TODO: this is a bit dirty.
# The value here gets added to the PATH of every service.
# We could consider copying the systemd lib from NixOS and removing the bits
# that are not relevant to us, like this option.
package = lib.mkOption {
type = lib.types.oneOf [ lib.types.str lib.types.path lib.types.package ];
default = pkgs.systemdMinimal;
};
globalEnvironment = lib.mkOption {
type = with lib.types; attrsOf (nullOr (oneOf [ str path package ]));
default = { };
example = { TZ = "CET"; };
description = lib.mdDoc ''
Environment variables passed to *all* systemd units.
'';
};
units = lib.mkOption {
description = lib.mdDoc "Definition of systemd units.";
@ -106,7 +124,6 @@ in
};
config = {
system-manager = {
systemd = {
timers =
lib.mapAttrs
@ -140,7 +157,7 @@ in
let
allowCollisions = false;
enabledUnits = cfg.units;
enabledUnits = lib.filterAttrs (_: unit: unit.enable) cfg.units;
in
{
"systemd/system".source = pkgs.runCommand "system-manager-units"
@ -188,5 +205,4 @@ in
'';
};
};
};
}