From 2152b61b5b6a8f499d19de01a9e275bccae8ed4e Mon Sep 17 00:00:00 2001 From: billy4479 Date: Sun, 29 Dec 2024 15:41:27 +0100 Subject: [PATCH] Split flake in multiple files --- flake.nix | 171 ++-------------------------------- flake/createAndMergeHosts.nix | 107 +++++++++++++++++++++ flake/system.nix | 68 ++++++++++++++ 3 files changed, 183 insertions(+), 163 deletions(-) create mode 100644 flake/createAndMergeHosts.nix create mode 100644 flake/system.nix diff --git a/flake.nix b/flake.nix index 7edbb16..39f6fb0 100644 --- a/flake.nix +++ b/flake.nix @@ -61,188 +61,33 @@ outputs = { nixpkgs, - home-manager, - catppuccin, - nix-vscode-extensions, - catppuccin-vsc, - plasma-manager, - spicetify-nix, server-tool, - sops-nix, ... }@inputs: let system = "x86_64-linux"; - # Shortcuts pkgs = import nixpkgs { config.allowUnfree = true; inherit system; }; - lib = nixpkgs.lib; - my-packages = import ./packages { inherit pkgs; } // { server-tool = server-tool.packages.${system}.default; }; - user = { - username = "billy"; - fullName = "Billy Panciotto"; - }; - - # https://github.com/catppuccin/nix/blob/5501cb508c2d4224d932a0b924d75454b68680bf/modules/lib/default.nix#L79 - mkUpper = - str: - (lib.toUpper (builtins.substring 0 1 str)) + (builtins.substring 1 (builtins.stringLength str) str); - - mkCatppuccinColors = - { flavor, accent }: - rec { - inherit flavor accent; - flavorWithAccent = if flavor == "frappe" then "Frappé" else flavor; - - upper = { - flavor = mkUpper flavor; - accent = mkUpper accent; - flavorWithAccent = mkUpper flavorWithAccent; - }; - }; - - # WARNING: the following part might look like a mess but it's actually quite straight forward. - # We have all this code because we want to share the same `specialArgs`/`extraSpecialArgs` between - # the nixos config and the home-manager config. - - # First we define the specialArgs/extraSpecialArgs for nixos config and home-manager - extraArgs = { - extraConfig = { - desktop = "kde"; - wayland = true; - bluetooth = true; - games = false; - isServer = false; - standaloneHomeManager = true; - - catppuccinColors = mkCatppuccinColors { - flavor = "frappe"; - accent = "green"; - }; - - inherit user; - }; - - extraPkgs = { - vscode-extensions = nix-vscode-extensions.extensions.${system}; - spicetifyPkgs = spicetify-nix.legacyPackages.${system}; - inherit catppuccin-vsc my-packages; - }; - - flakeInputs = inputs; + hosts = import ./flake/system.nix { + inherit + pkgs + system + my-packages + inputs + ; }; - # This function creates the flake output for a single host: - # we take a `hostname`, `extraSystemModules` (that we pass to NixOS), - # and `args` (that we pass to both to NixOS and home-manager). - hostFn = - { - hostname, - extraSystemModules ? [ ], - args ? { }, - }: - let - specialArgs = lib.recursiveUpdate extraArgs { extraConfig = (args // { inherit hostname; }); }; - defaultHomeManagerModules = [ - catppuccin.homeManagerModules.catppuccin - plasma-manager.homeManagerModules.plasma-manager - spicetify-nix.homeManagerModules.default - sops-nix.homeManagerModules.sops - ./user - ]; - in - { - nixosConfigurations.${hostname} = nixpkgs.lib.nixosSystem { - inherit system specialArgs; - modules = - [ - ./system - sops-nix.nixosModules.sops - ] - ++ lib.optionals (!specialArgs.extraConfig.standaloneHomeManager) [ - home-manager.nixosModules.home-manager - { - home-manager = { - useGlobalPkgs = true; - useUserPackages = true; - extraSpecialArgs = specialArgs; - users.${user.username}.imports = defaultHomeManagerModules; - }; - } - ] - ++ extraSystemModules; - }; - - homeConfigurations = - if specialArgs.extraConfig.standaloneHomeManager then - { - "${user.username}@${hostname}" = home-manager.lib.homeManagerConfiguration { - inherit pkgs; - extraSpecialArgs = specialArgs; - modules = defaultHomeManagerModules; - }; - } - else - { }; - }; - - # This function is like `lib.recursiveUpdate` but takes a list instead. - recursiveMerge = attrList: builtins.foldl' (a: b: lib.recursiveUpdate a b) { } attrList; - - # Now we map `hostProps` to "real" configurations using `hostFn` and we merge them. - createAndMergeHosts = hostProps: recursiveMerge (map hostFn hostProps); in { formatter.${system} = pkgs.nixfmt-rfc-style; } - # Finally we define our `hostProps`: - # these are the real configuration changes I want from one host to another. - // createAndMergeHosts [ - { - hostname = "nixbox"; - args = { - bluetooth = false; - }; - extraSystemModules = [ - ./system/hosts/vm - ]; - } - { - hostname = "portatilo"; - args = { }; - extraSystemModules = [ - ./system/hosts/portatilo - ]; - } - { - hostname = "computerone"; - args = { - desktop = "qtile"; - wayland = false; - games = true; - }; - extraSystemModules = [ - ./system/hosts/computerone - ]; - } - { - hostname = "serverone"; - args = { - isServer = true; - standaloneHomeManager = false; - bluetooth = false; - }; - extraSystemModules = [ - ./system/hosts/serverone - ]; - } - ]; + // hosts; } diff --git a/flake/createAndMergeHosts.nix b/flake/createAndMergeHosts.nix new file mode 100644 index 0000000..6970db1 --- /dev/null +++ b/flake/createAndMergeHosts.nix @@ -0,0 +1,107 @@ +{ + pkgs, + system, + my-packages, + inputs, +}: +let + lib = inputs.nixpkgs.lib; + + # https://github.com/catppuccin/nix/blob/5501cb508c2d4224d932a0b924d75454b68680bf/modules/lib/default.nix#L79 + mkUpper = + str: + (lib.toUpper (builtins.substring 0 1 str)) + (builtins.substring 1 (builtins.stringLength str) str); + + mkCatppuccinColors = + { flavor, accent }: + rec { + inherit flavor accent; + flavorWithAccent = if flavor == "frappe" then "Frappé" else flavor; + + upper = { + flavor = mkUpper flavor; + accent = mkUpper accent; + flavorWithAccent = mkUpper flavorWithAccent; + }; + }; + + # This function creates the flake output for a single host: + # we take a `hostname`, `extraSystemModules` (that we pass to NixOS), + # and `args` (that we pass to both to NixOS and home-manager). + hostFn = + defaultOptions: + { + hostname, + extraSystemModules ? [ ], + args, + }: + let + defaultedArgs = lib.recursiveUpdate defaultOptions args; + user = defaultedArgs.user; + + specialArgs = { + extraConfig = builtins.removeAttrs (lib.recursiveUpdate defaultedArgs { + catppuccinColors = mkCatppuccinColors defaultedArgs.catppuccin; + inherit hostname; + }) [ "catppuccin" ]; + + extraPkgs = { + vscode-extensions = inputs.nix-vscode-extensions.extensions.${system}; + spicetifyPkgs = inputs.spicetify-nix.legacyPackages.${system}; + inherit (inputs) catppuccin-vsc; + inherit my-packages; + }; + flakeInputs = inputs; + }; + + defaultHomeManagerModules = [ + inputs.catppuccin.homeManagerModules.catppuccin + inputs.plasma-manager.homeManagerModules.plasma-manager + inputs.spicetify-nix.homeManagerModules.default + inputs.sops-nix.homeManagerModules.sops + ../user + ]; + in + { + nixosConfigurations.${hostname} = inputs.nixpkgs.lib.nixosSystem { + inherit system specialArgs; + modules = + [ + ../system + inputs.sops-nix.nixosModules.sops + ] + ++ lib.optionals (!specialArgs.extraConfig.standaloneHomeManager) [ + inputs.home-manager.nixosModules.home-manager + { + home-manager = { + useGlobalPkgs = true; + useUserPackages = true; + extraSpecialArgs = specialArgs; + users.${user.username}.imports = defaultHomeManagerModules; + }; + } + ] + ++ extraSystemModules; + }; + + homeConfigurations = + if specialArgs.extraConfig.standaloneHomeManager then + { + "${user.username}@${hostname}" = inputs.home-manager.lib.homeManagerConfiguration { + inherit pkgs; + extraSpecialArgs = specialArgs; + modules = defaultHomeManagerModules; + }; + } + else + { }; + }; + + # This function is like `lib.recursiveUpdate` but takes a list instead. + recursiveMerge = attrList: builtins.foldl' (a: b: lib.recursiveUpdate a b) { } attrList; + + # Now we map `hostProps` to "real" configurations using `hostFn` and we merge them. + createAndMergeHosts = + defaultOptions: hostProps: recursiveMerge (map (prop: hostFn defaultOptions prop) hostProps); +in +createAndMergeHosts diff --git a/flake/system.nix b/flake/system.nix new file mode 100644 index 0000000..4c81009 --- /dev/null +++ b/flake/system.nix @@ -0,0 +1,68 @@ +{ + pkgs, + system, + my-packages, + inputs, +}@args: +let + defaultOptions = { + desktop = "kde"; + wayland = true; + bluetooth = true; + games = false; + isServer = false; + standaloneHomeManager = true; + + catppuccin = { + flavor = "frappe"; + accent = "green"; + }; + + user = { + username = "billy"; + fullName = "Billy Panciotto"; + }; + }; + + createAndMergeHosts = import ./createAndMergeHosts.nix args; +in +createAndMergeHosts defaultOptions [ + { + hostname = "nixbox"; + args = { + bluetooth = false; + }; + extraSystemModules = [ + ../system/hosts/vm + ]; + } + { + hostname = "portatilo"; + args = { }; + extraSystemModules = [ + ../system/hosts/portatilo + ]; + } + { + hostname = "computerone"; + args = { + desktop = "qtile"; + wayland = false; + games = true; + }; + extraSystemModules = [ + ../system/hosts/computerone + ]; + } + { + hostname = "serverone"; + args = { + isServer = true; + standaloneHomeManager = false; + bluetooth = false; + }; + extraSystemModules = [ + ../system/hosts/serverone + ]; + } +]