diff --git a/.gitmodules b/.gitmodules index bd90853..684b242 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "assets/compose"] path = assets/compose url = ssh://git@code.lazyworkhorse.net:2222/gortium/compose.git +[submodule "assets/dotfiles"] + path = assets/dotfiles + url = ssh://git@code.lazyworkhorse.net:2222/gortium/dotfiles.git diff --git a/flake.lock b/flake.lock index a5b95ac..71d5abd 100644 --- a/flake.lock +++ b/flake.lock @@ -44,6 +44,26 @@ "type": "github" } }, + "home-manager_2": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1755625756, + "narHash": "sha256-t57ayMEdV9g1aCfHzoQjHj1Fh3LDeyblceADm2hsLHM=", + "owner": "nix-community", + "repo": "home-manager", + "rev": "dd026d86420781e84d0732f2fa28e1c051117b59", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "home-manager", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1753939845, @@ -63,6 +83,7 @@ "root": { "inputs": { "agenix": "agenix", + "home-manager": "home-manager_2", "nixpkgs": "nixpkgs" } }, diff --git a/flake.nix b/flake.nix index af799ae..6716350 100644 --- a/flake.nix +++ b/flake.nix @@ -8,10 +8,14 @@ inputs.darwin.follows = ""; inputs.nixpkgs.follows = "nixpkgs"; }; + home-manager = { + url = "github:nix-community/home-manager"; + inputs.nixpkgs.follows = "nixpkgs"; + }; self.submodules = true; }; - outputs = { self, nixpkgs, agenix, ... }@inputs: + outputs = { self, nixpkgs, agenix, home-manager, ... }@inputs: let system = "x86_64-linux"; keys = import ./lib/keys.nix; @@ -36,10 +40,11 @@ modules = [ { nixpkgs.overlays = overlays; } agenix.nixosModules.default + home-manager.nixosModules.default ./hosts/lazyworkhorse/configuration.nix ./hosts/lazyworkhorse/hardware-configuration.nix ./modules/default.nix - ./users/gortium.nix + ./users/gortium ]; }; }; diff --git a/modules/nixos/services/default.nix b/modules/nixos/services/default.nix index 960abf0..c0d569a 100644 --- a/modules/nixos/services/default.nix +++ b/modules/nixos/services/default.nix @@ -1,6 +1,6 @@ -{ pkgs, lib, config, ... }: { - imports = - [ - ./systemd - ]; +{ + imports = [ + ./dotfiles.nix + ./systemd + ]; } diff --git a/modules/nixos/services/dotfiles.nix b/modules/nixos/services/dotfiles.nix new file mode 100644 index 0000000..b05fbe0 --- /dev/null +++ b/modules/nixos/services/dotfiles.nix @@ -0,0 +1,69 @@ +{ config, lib, pkgs, ... }: + +with lib; + +let + cfg = config.services.dotfiles; + stowDir = cfg.stowDir; + + # Function to recursively find all files in a directory + findFiles = dir: + let + files = builtins.attrNames (builtins.readDir dir); + in + concatMap (name: + let + path = dir + "/${name}"; + in + if (builtins.typeOf (builtins.readDir path) == "set") + then findFiles path + else [ path ] + ) files; + + # Get a list of all packages (directories) in the stow directory + stowPackages = builtins.attrNames (builtins.readDir stowDir); + + # Create an attribute set where each attribute is a package name + # and the value is a list of files to be linked. + homeManagerLinks = listToAttrs (map (pkg: + let + pkgPath = stowDir + "/${pkg}"; + files = findFiles pkgPath; + in + nameValuePair pkg (map (file: { + source = file; + target = removePrefix (pkgPath + "/") file; + }) files) + ) stowPackages); + +in +{ + options.services.dotfiles = { + enable = mkEnableOption "Enable dotfiles management"; + + stowDir = mkOption { + type = types.path; + description = "The directory where your stow packages are located."; + }; + + user = mkOption { + type = types.str; + description = "The user to manage dotfiles for."; + }; + }; + + config = mkIf cfg.enable { + home-manager.users.${cfg.user} = { + home.file = + let + allFiles = concatLists (attrValues homeManagerLinks); + in + listToAttrs (map (file: + nameValuePair file.target { + source = file.source; + } + ) allFiles); + }; + }; +} + diff --git a/users/gortium.nix b/users/gortium/default.nix similarity index 91% rename from users/gortium.nix rename to users/gortium/default.nix index 2c5a300..639a579 100644 --- a/users/gortium.nix +++ b/users/gortium/default.nix @@ -1,4 +1,5 @@ { pkgs, inputs, config, keys, ... }: { + home-manager.users.gortium = import ./home.nix; users.users.gortium = { isNormalUser = true; extraGroups = [ "wheel" "docker" ]; # Enable ‘sudo’ for the user. diff --git a/users/gortium/home.nix b/users/gortium/home.nix new file mode 100644 index 0000000..608f95f --- /dev/null +++ b/users/gortium/home.nix @@ -0,0 +1,12 @@ +{ pkgs, ... }: { + services.dotfiles = { + enable = true; + stowDir = ../../../assets/dotfiles; + user = "gortium"; + }; + + home.username = "gortium"; + home.homeDirectory = "/home/gortium"; + home.stateVersion = "23.11"; # Please change this to your version. + programs.home-manager.enable = true; +}