Major overhaul of the nix side of things, part II.
This commit is contained in:
parent
9759c2da12
commit
ce4cf7149d
7 changed files with 371 additions and 303 deletions
18
flake.lock
generated
18
flake.lock
generated
|
|
@ -121,6 +121,23 @@
|
||||||
"type": "github"
|
"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": {
|
"nixpkgs-stable": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1678872516,
|
"lastModified": 1678872516,
|
||||||
|
|
@ -169,6 +186,7 @@
|
||||||
"devshell": "devshell",
|
"devshell": "devshell",
|
||||||
"flake-utils": "flake-utils",
|
"flake-utils": "flake-utils",
|
||||||
"nixpkgs": "nixpkgs",
|
"nixpkgs": "nixpkgs",
|
||||||
|
"nixpkgs-nonflake": "nixpkgs-nonflake",
|
||||||
"pre-commit-hooks": "pre-commit-hooks",
|
"pre-commit-hooks": "pre-commit-hooks",
|
||||||
"rust-overlay": "rust-overlay",
|
"rust-overlay": "rust-overlay",
|
||||||
"treefmt-nix": "treefmt-nix"
|
"treefmt-nix": "treefmt-nix"
|
||||||
|
|
|
||||||
12
flake.nix
12
flake.nix
|
|
@ -1,6 +1,12 @@
|
||||||
{
|
{
|
||||||
inputs = {
|
inputs = {
|
||||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
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";
|
flake-utils.url = "github:numtide/flake-utils";
|
||||||
devshell = {
|
devshell = {
|
||||||
url = "github:numtide/devshell";
|
url = "github:numtide/devshell";
|
||||||
|
|
@ -41,6 +47,7 @@
|
||||||
outputs =
|
outputs =
|
||||||
{ self
|
{ self
|
||||||
, nixpkgs
|
, nixpkgs
|
||||||
|
, nixpkgs-nonflake
|
||||||
, flake-utils
|
, flake-utils
|
||||||
, rust-overlay
|
, rust-overlay
|
||||||
, crane
|
, crane
|
||||||
|
|
@ -49,7 +56,9 @@
|
||||||
, pre-commit-hooks
|
, pre-commit-hooks
|
||||||
,
|
,
|
||||||
}:
|
}:
|
||||||
(flake-utils.lib.eachDefaultSystem (system:
|
(flake-utils.lib.eachSystem
|
||||||
|
(with flake-utils.lib.system; [ x86_64-linux aarch64-linux ])
|
||||||
|
(system:
|
||||||
let
|
let
|
||||||
pkgs = import nixpkgs {
|
pkgs = import nixpkgs {
|
||||||
inherit system;
|
inherit system;
|
||||||
|
|
@ -180,6 +189,7 @@
|
||||||
{
|
{
|
||||||
lib = import ./nix/lib.nix {
|
lib = import ./nix/lib.nix {
|
||||||
inherit nixpkgs self;
|
inherit nixpkgs self;
|
||||||
|
nixosModules = "${nixpkgs-nonflake}/nixos";
|
||||||
};
|
};
|
||||||
|
|
||||||
systemConfigs.default = self.lib.makeSystemConfig {
|
systemConfigs.default = self.lib.makeSystemConfig {
|
||||||
|
|
|
||||||
31
nix/lib.nix
31
nix/lib.nix
|
|
@ -1,5 +1,6 @@
|
||||||
{ nixpkgs
|
{ nixpkgs # The nixpkgs flake
|
||||||
, self
|
, self # The system-manager flake
|
||||||
|
, nixosModules # The path to the nixos modules dir from nixpkgs
|
||||||
,
|
,
|
||||||
}:
|
}:
|
||||||
let
|
let
|
||||||
|
|
@ -16,22 +17,30 @@ in
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
inherit (self.packages.${system}) system-manager;
|
inherit (self.packages.${system}) system-manager;
|
||||||
|
|
||||||
# TODO can we call lib.evalModules directly instead of building a NixOS system?
|
# Module that sets additional module arguments
|
||||||
nixosConfig = (lib.nixosSystem {
|
extraArgsModule = { lib, config, pkgs, ... }: {
|
||||||
inherit system;
|
_module.args = {
|
||||||
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
|
utils = import "${nixosModules}/lib/utils.nix" {
|
||||||
|
inherit lib config pkgs;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
config = (lib.evalModules {
|
||||||
modules = [
|
modules = [
|
||||||
|
extraArgsModule
|
||||||
./modules/system-manager.nix
|
./modules/system-manager.nix
|
||||||
] ++ modules;
|
] ++ modules;
|
||||||
specialArgs = extraSpecialArgs;
|
|
||||||
}).config;
|
}).config;
|
||||||
|
|
||||||
returnIfNoAssertions = drv:
|
returnIfNoAssertions = drv:
|
||||||
let
|
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
|
in
|
||||||
if failedAssertions != [ ]
|
if failedAssertions != [ ]
|
||||||
then throw "\nFailed assertions:\n${lib.concatStringsSep "\n" (map (x: "- ${x}") 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 =
|
services =
|
||||||
lib.mapAttrs'
|
lib.mapAttrs'
|
||||||
|
|
@ -40,7 +49,7 @@ in
|
||||||
storePath =
|
storePath =
|
||||||
''${unit.unit}/${unitName}'';
|
''${unit.unit}/${unitName}'';
|
||||||
})
|
})
|
||||||
nixosConfig.system-manager.systemd.units;
|
config.systemd.units;
|
||||||
|
|
||||||
servicesPath = pkgs.writeTextFile {
|
servicesPath = pkgs.writeTextFile {
|
||||||
name = "services";
|
name = "services";
|
||||||
|
|
@ -64,7 +73,7 @@ in
|
||||||
|
|
||||||
filteredEntries = lib.filterAttrs
|
filteredEntries = lib.filterAttrs
|
||||||
(_name: etcFile: etcFile.enable)
|
(_name: etcFile: etcFile.enable)
|
||||||
nixosConfig.system-manager.environment.etc;
|
config.environment.etc;
|
||||||
|
|
||||||
srcDrvs = lib.mapAttrs addToStore filteredEntries;
|
srcDrvs = lib.mapAttrs addToStore filteredEntries;
|
||||||
|
|
||||||
|
|
@ -131,7 +140,7 @@ in
|
||||||
|
|
||||||
declare -a failed_assertions=()
|
declare -a failed_assertions=()
|
||||||
|
|
||||||
${mkAssertions nixosConfig.system-manager.preActivationAssertions}
|
${mkAssertions config.system-manager.preActivationAssertions}
|
||||||
|
|
||||||
if [ ''${#failed_assertions[@]} -ne 0 ]; then
|
if [ ''${#failed_assertions[@]} -ne 0 ]; then
|
||||||
for failed_assertion in ''${failed_assertions[@]}; do
|
for failed_assertion in ''${failed_assertions[@]}; do
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@
|
||||||
}:
|
}:
|
||||||
{
|
{
|
||||||
config = {
|
config = {
|
||||||
system-manager = {
|
|
||||||
environment.etc = {
|
environment.etc = {
|
||||||
foo = {
|
foo = {
|
||||||
text = ''
|
text = ''
|
||||||
|
|
@ -78,5 +77,4 @@
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
, ...
|
, ...
|
||||||
}:
|
}:
|
||||||
{
|
{
|
||||||
options.system-manager = {
|
options = {
|
||||||
environment.etc = lib.mkOption {
|
environment.etc = lib.mkOption {
|
||||||
default = { };
|
default = { };
|
||||||
example = lib.literalExpression ''
|
example = lib.literalExpression ''
|
||||||
|
|
@ -100,7 +100,6 @@
|
||||||
Changing this option takes precedence over `gid`.
|
Changing this option takes precedence over `gid`.
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
|
|
@ -110,7 +109,6 @@
|
||||||
in lib.mkDerivedConfig options.text (pkgs.writeText name')
|
in lib.mkDerivedConfig options.text (pkgs.writeText name')
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
));
|
));
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,31 @@
|
||||||
./systemd.nix
|
./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";
|
allowAnyDistro = lib.mkEnableOption "the usage of system-manager on untested distributions";
|
||||||
|
|
||||||
preActivationAssertions = lib.mkOption {
|
preActivationAssertions = lib.mkOption {
|
||||||
|
|
@ -29,14 +53,9 @@
|
||||||
default = { };
|
default = { };
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
};
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
# Avoid some standard NixOS assertions
|
|
||||||
boot = {
|
|
||||||
loader.grub.enable = false;
|
|
||||||
initrd.enable = false;
|
|
||||||
};
|
|
||||||
system.stateVersion = lib.mkDefault lib.trivial.release;
|
|
||||||
|
|
||||||
system-manager.preActivationAssertions = {
|
system-manager.preActivationAssertions = {
|
||||||
osVersion =
|
osVersion =
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,31 @@
|
||||||
}:
|
}:
|
||||||
|
|
||||||
let
|
let
|
||||||
cfg = config.system-manager.systemd;
|
cfg = config.systemd;
|
||||||
|
|
||||||
inherit (utils) systemdUtils;
|
inherit (utils) systemdUtils;
|
||||||
systemd-lib = utils.systemdUtils.lib;
|
systemd-lib = utils.systemdUtils.lib;
|
||||||
in
|
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 {
|
units = lib.mkOption {
|
||||||
description = lib.mdDoc "Definition of systemd units.";
|
description = lib.mdDoc "Definition of systemd units.";
|
||||||
|
|
@ -106,7 +124,6 @@ in
|
||||||
};
|
};
|
||||||
|
|
||||||
config = {
|
config = {
|
||||||
system-manager = {
|
|
||||||
systemd = {
|
systemd = {
|
||||||
timers =
|
timers =
|
||||||
lib.mapAttrs
|
lib.mapAttrs
|
||||||
|
|
@ -140,7 +157,7 @@ in
|
||||||
let
|
let
|
||||||
allowCollisions = false;
|
allowCollisions = false;
|
||||||
|
|
||||||
enabledUnits = cfg.units;
|
enabledUnits = lib.filterAttrs (_: unit: unit.enable) cfg.units;
|
||||||
in
|
in
|
||||||
{
|
{
|
||||||
"systemd/system".source = pkgs.runCommand "system-manager-units"
|
"systemd/system".source = pkgs.runCommand "system-manager-units"
|
||||||
|
|
@ -188,5 +205,4 @@ in
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue