Skip to content

Commit

Permalink
modules/nixos: add systemd services
Browse files Browse the repository at this point in the history
This adds systemd services configuration through hjem, under
`users.<username>.systemd.services`.
  • Loading branch information
nezia1 committed Feb 26, 2025
1 parent 8291092 commit 18e6f80
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
9 changes: 7 additions & 2 deletions modules/nixos/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@
hjemModule = submoduleWith {
description = "Hjem NixOS module";
class = "hjem";
specialArgs = {inherit pkgs lib;} // cfg.specialArgs;
specialArgs =
{
inherit pkgs lib;
systemConfig = config;
}
// cfg.specialArgs;
modules = concatLists [
[
({name, ...}: {
imports = [../common.nix];
imports = [../common.nix ./systemd.nix];

config = {
user = config.users.users.${name}.name;
Expand Down
45 changes: 45 additions & 0 deletions modules/nixos/systemd.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
lib,
pkgs,
systemConfig,
config,
...
}: let
inherit (lib.attrsets) nameValuePair mapAttrs';
inherit (lib.options) mkOption;

utils = import "${pkgs.path}/nixos/lib/utils.nix" {
inherit lib pkgs;
config = systemConfig;
};

inherit (utils) systemdUtils;
inherit (systemdUtils.lib) generateUnits serviceToUnit;

cfg = config.systemd;
in {
options.systemd = {
services = mkOption {
default = {};
type = systemdUtils.types.services;
description = "Definition of systemd per-user service units.";
};
units = mkOption {
description = "Definition of systemd per-user units.";
default = {};
type = systemdUtils.types.units;
};
};

config = {
systemd.units = mapAttrs' (n: v: nameValuePair "${n}.service" (serviceToUnit v)) cfg.services;
files.".config/systemd/user".source = generateUnits {
type = "user";
inherit (cfg) units;
upstreamUnits = []; # this is already done in the NixOS module
upstreamWants = []; # same here
packages = []; # unnecessary, we're using `generateUnits` at user level (no need for hooks or units for now)
inherit (systemConfig.systemd) package;
};
};
}

0 comments on commit 18e6f80

Please sign in to comment.