From aec3458fb55b4f86ad62444d6e087025d05ab4b9 Mon Sep 17 00:00:00 2001 From: Charlie Root Date: Fri, 1 Nov 2024 14:26:59 +0100 Subject: [PATCH] modules: add homix a --- modules/homix/module.nix | 96 ++++++++++++++++++++++++++++++++++++++++ modules/other/users.nix | 1 + 2 files changed, 97 insertions(+) create mode 100644 modules/homix/module.nix diff --git a/modules/homix/module.nix b/modules/homix/module.nix new file mode 100644 index 0000000..5ec0883 --- /dev/null +++ b/modules/homix/module.nix @@ -0,0 +1,96 @@ +# This is 1:1 copied from https://github.com/sioodmy/homix. +# I simply put it here because it's not changing in any way anymore and i save myself a flake input this way. + +{ + config, + lib, + pkgs, + ... +}: let + inherit (lib) mkOption mkEnableOption types filterAttrs attrValues mkIf mkDerivedConfig; + + inherit (builtins) map listToAttrs attrNames; +in { + options = { + homix = mkOption { + default = {}; + type = types.attrsOf (types.submodule ({ + name, + config, + options, + ... + }: { + options = { + path = mkOption { + type = types.str; + description = '' + Path to the file relative to the $HOME directory. + If not defined, name of attribute set will be used. + ''; + }; + source = mkOption { + type = types.path; + description = "Path of the source file or directory."; + }; + text = mkOption { + default = null; + type = types.nullOr types.lines; + description = "Text of the file."; + }; + }; + config = { + path = lib.mkDefault name; + source = mkIf (config.text != null) ( + let + name' = "homix-" + lib.replaceStrings ["/"] ["-"] name; + in + mkDerivedConfig options.text (pkgs.writeText name') + ); + }; + })); + }; + users.users = mkOption { + type = types.attrsOf (types.submodule { + options.homix = mkEnableOption "Enable homix for selected user"; + }); + }; + }; + + config = let + # list of users managed by homix + users = attrNames (filterAttrs (name: user: user.homix) config.users.users); + + homix-link = let + files = map (f: '' + FILE=$HOME/${f.path} + mkdir -p $(dirname $FILE) + ln -sf ${f.source} $FILE + '') (attrValues config.homix); + in + pkgs.writeShellScript "homix-link" '' + #!/bin/sh + ${builtins.concatStringsSep "\n" files} + ''; + + mkService = user: { + name = "homix-${user}"; + value = { + wantedBy = ["multi-user.target"]; + description = "Setup homix environment for ${user}."; + serviceConfig = { + Type = "oneshot"; + User = "${user}"; + ExecStart = "${homix-link}"; + }; + environment = { + # epic systemd momento + HOME = config.users.users.${user}.home; + }; + }; + }; + + services = listToAttrs (map mkService users); + in { + systemd.services = services; + }; +} diff --git a/modules/other/users.nix b/modules/other/users.nix index b702002..e20ee0f 100644 --- a/modules/other/users.nix +++ b/modules/other/users.nix @@ -14,6 +14,7 @@ in { "audio" "nix" ]; + homix = true; # hashedPasswordFile = "/etc/passwords/cr"; }; # root.hashedPasswordFile = "/persist/passwords/root";