Skip to content

Commit

Permalink
Move convertServiceToNixOS to common lib module (#74)
Browse files Browse the repository at this point in the history
- Simplifies NixOS installation process to not require also extending lib with
  nix-snapshotter's lib output.
  • Loading branch information
elpdt852 committed Sep 6, 2023
1 parent 93cd08f commit 4564b74
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 44 deletions.
11 changes: 1 addition & 10 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,8 @@
};

outputs = inputs@{ nixpkgs, flake-parts, ... }:
let
lib = nixpkgs.lib.extend(_: lib:
import ./lib { inherit lib; }
);

in flake-parts.lib.mkFlake {
inherit inputs;
specialArgs = { inherit lib; };
} {
flake-parts.lib.mkFlake { inherit inputs; } {
systems = [ "x86_64-linux" ];
imports = [ ./modules ];
flake = { inherit lib; };
};
}
17 changes: 0 additions & 17 deletions lib/default.nix

This file was deleted.

14 changes: 14 additions & 0 deletions modules/common/nix-snapshotter-lib.nix
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,19 @@ let
# home-manager modules, the easiest transformation is home-manager -> NixOS,
# which is why the services are written as such.

# Converts a home-manager systemd user service to a NixOS systemd user
# service. Since home-manager style services map closer to raw systemd
# service specification, it's easier to transform in this direction.
convertServiceToNixOS = unit:
{
serviceConfig = lib.optionalAttrs (unit?Service) unit.Service;
unitConfig = lib.optionalAttrs (unit?Unit) unit.Unit;
} // (lib.optionalAttrs (unit?Install.WantedBy) {
# Only `WantedBy` is supported by NixOS as [Install] fields are not
# supported, due to its stateful nature.
wantedBy = unit.Install.WantedBy;
});

mkNixSnapshotterService = {
Service = {
Type = "notify";
Expand Down Expand Up @@ -172,6 +185,7 @@ in {
default = {
inherit options;
inherit settingsFormat;
inherit convertServiceToNixOS;
inherit mkContainerdSettings;
inherit mkNixSnapshotterService;
inherit mkRootlessNixSnapshotterService;
Expand Down
8 changes: 3 additions & 5 deletions modules/nixos/containerd-rootless.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{ config, lib, ... }:
let
inherit (lib.home-manager)
convertServiceToNixOS
;

cfg = config.virtualisation.containerd.rootless;

ns-lib = config.services.nix-snapshotter.lib;

proxyEnv = config.networking.proxy.envVars;

in {
Expand All @@ -19,7 +17,7 @@ in {
'';

systemd.user.services.containerd = lib.recursiveUpdate
(convertServiceToNixOS (cfg.lib.mkRootlessContainerdService cfg))
(ns-lib.convertServiceToNixOS (cfg.lib.mkRootlessContainerdService cfg))
{ environment = proxyEnv; };
};
}
8 changes: 2 additions & 6 deletions modules/nixos/nix-snapshotter-rootless.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
{ config, lib, ... }:
let
inherit (lib.home-manager)
convertServiceToNixOS
;

cfg = config.services.nix-snapshotter.rootless;

ns-lib = config.services.nix-snapshotter.lib;
Expand All @@ -29,12 +25,12 @@ in {
'');

systemd.user.services.nix-snapshotter = lib.recursiveUpdate
(convertServiceToNixOS (ns-lib.mkRootlessNixSnapshotterService cfg))
(ns-lib.convertServiceToNixOS (ns-lib.mkRootlessNixSnapshotterService cfg))
{ inherit (cfg) path; };
}
{
systemd.user.services.preload-containerd-images = lib.recursiveUpdate
(convertServiceToNixOS (ns-lib.mkRootlessPreloadContainerdImageService cfg))
(ns-lib.convertServiceToNixOS (ns-lib.mkRootlessPreloadContainerdImageService cfg))
{ environment.CONTAINERD_ADDRESS = "%t/containerd/containerd.sock"; };
}
]);
Expand Down
8 changes: 2 additions & 6 deletions modules/nixos/nix-snapshotter.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ let
types
;

inherit (lib.home-manager)
convertServiceToNixOS
;

cfg = config.services.nix-snapshotter;

ns-lib = cfg.lib;
Expand Down Expand Up @@ -57,7 +53,7 @@ in {
};

systemd.services.nix-snapshotter = lib.recursiveUpdate
(convertServiceToNixOS ns-lib.mkNixSnapshotterService)
(ns-lib.convertServiceToNixOS ns-lib.mkNixSnapshotterService)
{
inherit (cfg) path;
description = "nix-snapshotter - containerd snapshotter that understands nix store paths natively";
Expand All @@ -69,7 +65,7 @@ in {
}
(lib.mkIf (cfg.preloadContainerdImages != []) {
systemd.services.preload-containerd-images = lib.recursiveUpdate
(convertServiceToNixOS (ns-lib.mkPreloadContainerdImageService cfg))
(ns-lib.convertServiceToNixOS (ns-lib.mkPreloadContainerdImageService cfg))
{
description = "Preload images to containerd";
wantedBy = [ "multi-user.target" ];
Expand Down

0 comments on commit 4564b74

Please sign in to comment.