From fe306085f4a12255edc43ed350b3ce836abdee70 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Thu, 18 Jul 2024 16:57:22 +0200 Subject: [PATCH] base: move some shared configuration to shared --- machines/thaylen-city/default.nix | 78 ++++------------------------- modules/darwin/base/default.nix | 12 +++++ modules/darwin/base/nix/default.nix | 9 ++++ modules/darwin/default.nix | 8 ++- modules/nixos/base/default.nix | 37 +++----------- modules/nixos/base/nix/default.nix | 78 ++--------------------------- modules/nixos/base/zsh/default.nix | 57 +++------------------ modules/shared/base/default.nix | 65 ++++++++++++++++++++++++ modules/shared/base/nix/default.nix | 73 +++++++++++++++++++++++++++ modules/shared/base/zsh/default.nix | 75 +++++++++++++++++++++++++++ modules/shared/default.nix | 8 ++- 11 files changed, 273 insertions(+), 227 deletions(-) create mode 100644 modules/darwin/base/default.nix create mode 100644 modules/darwin/base/nix/default.nix create mode 100644 modules/shared/base/default.nix create mode 100644 modules/shared/base/nix/default.nix create mode 100644 modules/shared/base/zsh/default.nix diff --git a/machines/thaylen-city/default.nix b/machines/thaylen-city/default.nix index 222365e3..48c9d78d 100644 --- a/machines/thaylen-city/default.nix +++ b/machines/thaylen-city/default.nix @@ -1,10 +1,15 @@ { pkgs, ... }: { + chvp = { + homeStateVersion = "24.11"; + systemStateVersion = 4; + username = "charlotte.vanpetegem"; + }; + networking.computerName = "Thaylen City"; + homebrew = { enable = true; - brews = [ - ]; casks = [ "docker" "libreoffice" @@ -18,25 +23,7 @@ upgrade = true; }; }; - networking.computerName = "Thaylen City"; - nix = { - extraOptions = '' - keep-outputs = true - keep-derivations = true - ''; - configureBuildUsers = true; - }; - programs = { - bash.enable = false; - zsh.enable = true; - }; security.pam.enableSudoTouchIdAuth = true; - services.nix-daemon.enable = true; - users.users."charlotte.vanpetegem" = { - name = "charlotte.vanpetegem"; - home = "/Users/charlotte.vanpetegem"; - shell = pkgs.zsh; - }; system = { defaults = { dock = { @@ -65,54 +52,12 @@ remapCapsLockToEscape = true; }; startup.chime = false; - stateVersion = 4; }; home-manager.users."charlotte.vanpetegem" = { programs = { - direnv = { - enable = true; - enableZshIntegration = true; - nix-direnv = { - enable = true; - }; - config = { - global = { - load_dotenv = true; - }; - }; - }; - zsh = { - enable = true; - autocd = true; - autosuggestion.enable = true; - dotDir = ".config/zsh"; - history = { - expireDuplicatesFirst = true; - path = "$HOME/.local/share/zsh/history"; - }; - oh-my-zsh = { - enable = true; - plugins = [ - "autojump" - "common-aliases" - "extract" - "history-substring-search" - "git" - "tmux" - ]; - theme = "robbyrussell"; - }; - sessionVariables = { - DEFAULT_USER = "charlotte.vanpetegem"; - # Until I get emacs set up on this machine - EDITOR = "vim"; - }; - shellAliases = { - gupd = "gfa && gprom"; - }; - syntaxHighlighting.enable = true; - }; + # Until I get emacs set up on this machine + zsh.sessionVariables.EDITOR = "vim"; git = { enable = true; extraConfig = { @@ -138,14 +83,9 @@ home = { packages = with pkgs; [ - autojump - coreutils docker-compose - nix-direnv - ripgrep tmux ]; - stateVersion = "24.11"; }; }; } diff --git a/modules/darwin/base/default.nix b/modules/darwin/base/default.nix new file mode 100644 index 00000000..d87c8a93 --- /dev/null +++ b/modules/darwin/base/default.nix @@ -0,0 +1,12 @@ +{ config, ... }: + +let + username = config.chvp.username; +in +{ + imports = [ + ./nix + ]; + + users.users.${username}.home = "/Users/${username}"; +} diff --git a/modules/darwin/base/nix/default.nix b/modules/darwin/base/nix/default.nix new file mode 100644 index 00000000..79321e7a --- /dev/null +++ b/modules/darwin/base/nix/default.nix @@ -0,0 +1,9 @@ +{ config, ... }: + +{ + nix = { + configureBuildUsers = true; + gc.interval = if config.chvp.base.nix.slowGc then { Hour = 0; Minute = 0; } else { Minute = 0; }; + }; + services.nix-daemon.enable = true; +} diff --git a/modules/darwin/default.nix b/modules/darwin/default.nix index c915eb0a..54e8d2d3 100644 --- a/modules/darwin/default.nix +++ b/modules/darwin/default.nix @@ -1 +1,7 @@ -{ ... }: { } +{ ... }: + +{ + imports = [ + ./base + ]; +} diff --git a/modules/nixos/base/default.nix b/modules/nixos/base/default.nix index 533bdfc8..f9ac52cc 100644 --- a/modules/nixos/base/default.nix +++ b/modules/nixos/base/default.nix @@ -17,46 +17,22 @@ ./zsh ]; - options.chvp = { - stateVersion = lib.mkOption { - example = "20.09"; - }; - - dataPrefix = lib.mkOption { - default = ""; - example = "/data"; - }; - - cachePrefix = lib.mkOption { - default = ""; - example = "/cache"; - }; - }; - config = { - home-manager.useGlobalPkgs = true; - - system = { - stateVersion = config.chvp.stateVersion; - autoUpgrade = { - enable = true; - flake = "gitlab:chvp/nixos-config?host=git.chvp.be"; - dates = "01/4:00"; - randomizedDelaySec = "10min"; - }; + system.autoUpgrade = { + enable = true; + flake = "gitlab:chvp/nixos-config?host=git.chvp.be"; + dates = "01/4:00"; + randomizedDelaySec = "10min"; }; home-manager.users = { charlotte = { ... }: { - home.stateVersion = config.chvp.stateVersion; systemd.user.sessionVariables = config.home-manager.users.charlotte.home.sessionVariables; }; root = { ... }: { - home.stateVersion = config.chvp.stateVersion; + home.stateVersion = config.chvp.homeStateVersion; }; }; - environment.systemPackages = with pkgs; [ git htop moreutils ncdu ripgrep unzip zip ]; - boot.kernelParams = [ "mitigations=off" ]; console = { @@ -120,7 +96,6 @@ charlotte = { isNormalUser = true; home = "/home/charlotte"; - description = "Charlotte Van Petegem"; extraGroups = [ "systemd-journal" ]; hashedPasswordFile = config.age.secrets."passwords/users/charlotte".path; }; diff --git a/modules/nixos/base/nix/default.nix b/modules/nixos/base/nix/default.nix index 24b49da7..3087096e 100644 --- a/modules/nixos/base/nix/default.nix +++ b/modules/nixos/base/nix/default.nix @@ -1,39 +1,6 @@ { config, lib, pkgs, ... }: -let - baseDirenv = { - programs.direnv = { - enable = true; - enableZshIntegration = true; - nix-direnv.enable = true; - }; - }; - baseNixIndex = { - programs.command-not-found.enable = false; - programs.nix-index = { - enable = true; - package = config.programs.nix-index.package; - }; - }; -in { - options.chvp.base.nix = { - enableDirenv = lib.mkOption { - default = true; - example = false; - }; - slowGc = lib.mkOption { - default = false; - example = true; - }; - # Used in /flake.nix, since we have to use it at nixpkgs import time - unfreePackages = lib.mkOption { - default = [ ]; - example = [ "teams" ]; - }; - }; - config = { - programs.command-not-found.enable = false; chvp.base = { emacs.extraConfig = [ '' @@ -48,48 +15,9 @@ in :config (direnv-mode) ) ''; - zfs = { - homeLinks = - (lib.optional config.chvp.base.nix.enableDirenv { path = ".local/share/direnv"; type = "cache"; }); - systemLinks = - (lib.optional config.chvp.base.nix.enableDirenv { path = "/root/.local/share/direnv"; type = "cache"; }); - }; + zfs.homeLinks = (lib.optional config.chvp.base.nix.enableDirenv { path = ".local/share/direnv"; type = "cache"; }); }; - - nix = { - gc = { - automatic = true; - dates = if config.chvp.base.nix.slowGc then "daily" else "hourly"; - options = "--delete-older-than 7d"; - }; - settings = { - auto-optimise-store = true; - substituters = [ - "https://cache.nixos.org" - "https://accentor.cachix.org" - "https://chvp.cachix.org" - "https://lanzaboote.cachix.org" - "https://nix-community.cachix.org" - ]; - trusted-public-keys = [ - "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" - "accentor.cachix.org-1:QP+oJwzmeq5Fsyp4Vk501UgUSbl5VIna/ard/XOePH8=" - "chvp.cachix.org-1:eIG26KkeA+R3tCpvmaayA9i3KVVL06G+qB5ci4dHBT4=" - "lanzaboote.cachix.org-1:Nt9//zGmqkg1k5iu+B3bkj3OmHKjSw9pvf3faffLLNk=" - "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=" - ]; - trusted-users = [ "@wheel" ]; - }; - extraOptions = lib.mkIf config.chvp.base.nix.enableDirenv '' - keep-outputs = true - keep-derivations = true - ''; - }; - - home-manager.users.charlotte = { ... }: - lib.recursiveUpdate - (lib.optionalAttrs config.chvp.base.nix.enableDirenv baseDirenv) - baseNixIndex; - home-manager.users.root = { ... }: baseNixIndex; + nix.gc.dates = if config.chvp.base.nix.slowGc then "daily" else "hourly"; + programs.command-not-found.enable = false; }; } diff --git a/modules/nixos/base/zsh/default.nix b/modules/nixos/base/zsh/default.nix index 0708246f..003d8052 100644 --- a/modules/nixos/base/zsh/default.nix +++ b/modules/nixos/base/zsh/default.nix @@ -2,41 +2,8 @@ let base = (home: { - home.packages = [ pkgs.autojump ]; programs.zsh = { - enable = true; - autosuggestion.enable = true; - syntaxHighlighting.enable = true; - autocd = true; - dotDir = ".config/zsh"; - history = { - expireDuplicatesFirst = true; - path = "${config.chvp.cachePrefix}${home}/.local/share/zsh/history"; - }; initExtra = '' - nshell() { - local -a drvs - for attr in "$@"; do - drvs+=(nixpkgs#$attr) - done - local paths="$(nix build --no-link --print-out-paths $drvs)" - for p in $paths; do - export PATH="$p/bin:$PATH" - done - } - - nrun() { - local drv="$1" - shift 1 - nix run nixpkgs#$drv $@ - } - - nsrun() { - local drv="$1" - shift 1 - nix shell nixpkgs#$drv -c $@ - } - lightmode() { printf "\033]10;rgb:4c/4f/69\007" printf "\033]11;rgb:ef/f1/f5\007" @@ -101,27 +68,17 @@ let fi fi ''; - sessionVariables = { DEFAULT_USER = "charlotte"; }; - oh-my-zsh = { - enable = true; - plugins = [ - "autojump" - "common-aliases" - "extract" - "history-substring-search" - "git" - "systemd" - "tmux" - ]; - theme = "robbyrussell"; - }; }; }); in { + chvp.base.zsh.usersToConfigure = [ "charlotte" "root" ]; chvp.base.zfs.systemLinks = [{ path = "/root/.local/share/autojump"; type = "cache"; }]; chvp.base.zfs.homeLinks = [{ path = ".local/share/autojump"; type = "cache"; }]; - home-manager.users.charlotte = { ... }: (base "/home/charlotte"); - home-manager.users.root = { ... }: (base "/root"); - programs.zsh.enable = true; +} // { + home-manager.users = builtins.foldl' (a: b: a // b) { } ( + builtins.map + (name: { "${name}" = { ... }: (base config.users.users.${name}.home); }) + config.chvp.base.zsh.usersToConfigure + ); } diff --git a/modules/shared/base/default.nix b/modules/shared/base/default.nix new file mode 100644 index 00000000..076fee80 --- /dev/null +++ b/modules/shared/base/default.nix @@ -0,0 +1,65 @@ +{ config, lib, pkgs, ... }: + +{ + imports = [ + ./nix + ./zsh + ]; + + options.chvp = { + cachePrefix = lib.mkOption { + default = ""; + example = "/cache"; + }; + dataPrefix = lib.mkOption { + default = ""; + example = "/data"; + }; + stateVersion = lib.mkOption { + example = "20.09"; + }; + homeStateVersion = lib.mkOption { + default = config.chvp.stateVersion; + }; + systemStateVersion = lib.mkOption { + default = config.chvp.stateVersion; + }; + username = lib.mkOption { + default = "charlotte"; + example = "charlotte.vanpetegem"; + }; + }; + + config = + let + username = config.chvp.username; + in + { + environment.systemPackages = with pkgs; [ + coreutils + git + htop + moreutils + ncdu + ripgrep + unzip + zip + ]; + + home-manager = { + useGlobalPkgs = true; + users = { + "${username}" = { ... }: { + home.stateVersion = config.chvp.homeStateVersion; + }; + }; + }; + + system.stateVersion = config.chvp.systemStateVersion; + + users.users.${username} = { + description = "Charlotte Van Petegem"; + shell = pkgs.zsh; + }; + }; +} diff --git a/modules/shared/base/nix/default.nix b/modules/shared/base/nix/default.nix new file mode 100644 index 00000000..9987f870 --- /dev/null +++ b/modules/shared/base/nix/default.nix @@ -0,0 +1,73 @@ +{ config, lib, pkgs, ... }: + +let + baseDirenv = { + programs.direnv = { + enable = true; + enableZshIntegration = true; + nix-direnv.enable = true; + config.global.load_dotenv = true; + }; + }; + baseNixIndex = { + programs.command-not-found.enable = false; + programs.nix-index = { + enable = true; + package = config.programs.nix-index.package; + }; + }; + username = config.chvp.username; +in +{ + options.chvp.base.nix = { + enableDirenv = lib.mkOption { + default = true; + example = false; + }; + slowGc = lib.mkOption { + default = false; + example = true; + }; + # Used in /flake.nix, since we have to use it at nixpkgs import time + unfreePackages = lib.mkOption { + default = [ ]; + example = [ "teams" ]; + }; + }; + + config = { + nix = { + gc = { + automatic = true; + options = "--delete-older-than 7d"; + }; + settings = { + auto-optimise-store = true; + substituters = [ + "https://cache.nixos.org" + "https://accentor.cachix.org" + "https://chvp.cachix.org" + "https://lanzaboote.cachix.org" + "https://nix-community.cachix.org" + ]; + trusted-public-keys = [ + "cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY=" + "accentor.cachix.org-1:QP+oJwzmeq5Fsyp4Vk501UgUSbl5VIna/ard/XOePH8=" + "chvp.cachix.org-1:eIG26KkeA+R3tCpvmaayA9i3KVVL06G+qB5ci4dHBT4=" + "lanzaboote.cachix.org-1:Nt9//zGmqkg1k5iu+B3bkj3OmHKjSw9pvf3faffLLNk=" + "nix-community.cachix.org-1:mB9FSh9qf2dCimDSUo8Zy7bkq5CX+/rkCWyvRCYg3Fs=" + ]; + trusted-users = [ username ]; + }; + extraOptions = lib.mkIf config.chvp.base.nix.enableDirenv '' + keep-outputs = true + keep-derivations = true + ''; + }; + + home-manager.users.${username} = { ... }: + lib.recursiveUpdate + (lib.optionalAttrs config.chvp.base.nix.enableDirenv baseDirenv) + baseNixIndex; + }; +} diff --git a/modules/shared/base/zsh/default.nix b/modules/shared/base/zsh/default.nix new file mode 100644 index 00000000..787c3e95 --- /dev/null +++ b/modules/shared/base/zsh/default.nix @@ -0,0 +1,75 @@ +{ config, lib, pkgs, ... }: + +let + username = config.chvp.username; + base = (home: { + home.packages = [ pkgs.autojump ]; + programs.zsh = { + enable = true; + autosuggestion.enable = true; + syntaxHighlighting.enable = true; + autocd = true; + dotDir = ".config/zsh"; + history = { + expireDuplicatesFirst = true; + path = "${config.chvp.cachePrefix}${home}/.local/share/zsh/history"; + }; + initExtra = '' + nshell() { + local -a drvs + for attr in "$@"; do + drvs+=(nixpkgs#$attr) + done + local paths="$(nix build --no-link --print-out-paths $drvs)" + for p in $paths; do + export PATH="$p/bin:$PATH" + done + } + + nrun() { + local drv="$1" + shift 1 + nix run nixpkgs#$drv $@ + } + + nsrun() { + local drv="$1" + shift 1 + nix shell nixpkgs#$drv -c $@ + } + ''; + shellAliases = { + gupd = "gfa && gprom"; + }; + sessionVariables = { DEFAULT_USER = "charlotte"; }; + oh-my-zsh = { + enable = true; + plugins = [ + "autojump" + "common-aliases" + "extract" + "history-substring-search" + "git" + "systemd" + "tmux" + ]; + theme = "robbyrussell"; + }; + }; + }); +in +{ + options.chvp.base.zsh.usersToConfigure = lib.mkOption { + default = [ username ]; + }; + + config = { + programs.zsh.enable = true; + } // { + home-manager.users = builtins.foldl' (a: b: a // b) { } ( + builtins.map + (name: { "${name}" = { ... }: (base config.users.users.${name}.home); }) + config.chvp.base.zsh.usersToConfigure + ); + }; +} diff --git a/modules/shared/default.nix b/modules/shared/default.nix index c915eb0a..54e8d2d3 100644 --- a/modules/shared/default.nix +++ b/modules/shared/default.nix @@ -1 +1,7 @@ -{ ... }: { } +{ ... }: + +{ + imports = [ + ./base + ]; +}