Skip to content

Commit

Permalink
refactor(modules)!: auto import modules & improve passing of arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
getchoo committed Nov 3, 2023
1 parent a0e5082 commit 406ca9f
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 76 deletions.
6 changes: 4 additions & 2 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import ./_sources/generated.nix { inherit (pkgs) fetchgit fetchurl fetchFromGitHub dockerTools; };
in
builtins.mapAttrs (_: p: p.src) s;

moduleArgs = { inherit sources; };
in
{
checks = forAllSystems (pkgs: lib.optionalAttrs pkgs.stdenv.isLinux {
Expand All @@ -37,9 +39,9 @@

formatter = forAllSystems (pkgs: pkgs.nixpkgs-fmt);

homeManagerModules.catppuccin = import ./modules/home-manager { inherit inputs sources; };
homeManagerModules.catppuccin = import ./modules/home-manager moduleArgs;

nixosModules.catppuccin = import ./modules/nixos { inherit inputs sources; };
nixosModules.catppuccin = import ./modules/nixos moduleArgs;

packages = forAllSystems (pkgs:
let
Expand Down
45 changes: 2 additions & 43 deletions modules/home-manager/default.nix
Original file line number Diff line number Diff line change
@@ -1,45 +1,4 @@
{ inputs, ... }@flakeArgs: { lib, pkgs, ... }@systemArgs:
let
extendedLib = import ../lib/mkExtLib.nix inputs.nixpkgs.lib (flakeArgs // systemArgs);
inherit (extendedLib) ctp;
in
flakeArgs: { lib, pkgs, ... }@systemArgs:
{
imports =
let
files = [
./alacritty.nix
./bat.nix
./bottom.nix
./btop.nix
./fish.nix
./kitty.nix
./lazygit.nix
./starship.nix
./helix.nix
./hyprland.nix
./glamour.nix
./gtk.nix
./mako.nix
./neovim.nix
./micro.nix
./polybar.nix
./sway.nix
./tmux.nix
./zathura.nix
];
in
extendedLib.ctp.mapModules extendedLib files;

options.catppuccin = {
flavour = lib.mkOption {
type = ctp.types.flavourOption;
default = "latte";
description = "Global Catppuccin flavour";
};
accent = lib.mkOption {
type = ctp.types.accentOption;
default = "teal";
description = "Global Catppuccin accent";
};
};
imports = import ../lib/mkImports.nix (flakeArgs // systemArgs) ./.;
}
15 changes: 15 additions & 0 deletions modules/home-manager/globals.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{lib, ...}: {
options.catppuccin = {
flavour = lib.mkOption {
type = lib.ctp.types.flavourOption;
default = "latte";
description = "Global Catppuccin flavour";
};

accent = lib.mkOption {
type = lib.ctp.types.accentOption;
default = "teal";
description = "Global Catppuccin accent";
};
};
}
1 change: 1 addition & 0 deletions modules/home-manager/zathura.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
, pkgs
, lib
, sources
, ...
}:
let
cfg = config.programs.zathura.catppuccin;
Expand Down
30 changes: 20 additions & 10 deletions modules/lib/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lib: { config, pkgs, sources, ... }:
{ config, lib, pkgs, sources, ... }@args:
let
# string -> type -> string -> a -> a
# this is an internal function and shouldn't be
Expand Down Expand Up @@ -80,15 +80,25 @@ in
in
fromJSON (readFile json);

# a -> a -> [path] -> [path]
# this imports a list of paths while inheriting
# multiple attributes
mapModules = extendedLib:
map (m: (import m {
inherit config pkgs;
sources = sources pkgs;
lib = extendedLib;
}));
# a -> path -> [path]
# this imports all files in a directory (besides default.nix)
# with our modified arguments
# `a` should be nixpkgs.lib extended with this file
mapModules = extendedLib: dir: lib.pipe dir [
builtins.readDir
builtins.attrNames

(builtins.filter (
n: !(builtins.elem n [ "default.nix" ])
))

(map (
f: _: import "${dir}/${f}" (args // {
sources = sources pkgs;
lib = extendedLib;
})
))
];

# string -> a -> a
# this creates a basic attrset only containing an
Expand Down
1 change: 0 additions & 1 deletion modules/lib/mkExtLib.nix

This file was deleted.

16 changes: 16 additions & 0 deletions modules/lib/mkImports.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{lib, pkgs, sources, ...}@args:
dir: lib.pipe dir [
builtins.readDir
builtins.attrNames

(builtins.filter (
n: !(builtins.elem n [ "default.nix" ])
))

(map (
f: _: import "${dir}/${f}" (args // {
sources = sources pkgs;
lib = lib.extend (_: _: { ctp = import ./. args; });
})
))
]
22 changes: 2 additions & 20 deletions modules/nixos/default.nix
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
{ inputs, ... }@flakeArgs: { lib, pkgs, ... }@systemArgs:
let
extendedLib = import ../lib/mkExtLib.nix inputs.nixpkgs.lib (systemArgs // flakeArgs);
in
flakeArgs: { lib, pkgs, ... }@systemArgs:
{
imports =
let
files = [
./grub.nix
];
in
extendedLib.ctp.mapModules extendedLib files;


options.catppuccin = with extendedLib; {
flavour = mkOption {
type = ctp.types.flavourOption;
default = "latte";
description = "Global Catppuccin flavour";
};
};
imports = import ../lib/mkImports.nix (systemArgs // flakeArgs) ./.;
}
9 changes: 9 additions & 0 deletions modules/nixos/globals.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{lib, ...}: {
options.catppuccin = {
flavour = lib.mkOption {
type = lib.ctp.types.flavourOption;
default = "latte";
description = "Global Catppuccin flavour";
};
};
}

0 comments on commit 406ca9f

Please sign in to comment.