From b83be2da0150c3ae84d1c0c6916f7056f0a8a33e Mon Sep 17 00:00:00 2001 From: guangtao Date: Wed, 11 Oct 2023 01:22:39 -0700 Subject: [PATCH] feat(darwinProfiles): add homebrew --- docs/org/nixosProfiles.org | 1 - lib/exporter/flakeOutputs.nix | 3 + lib/exporter/pops.nix | 11 +++ local/cells/repo/configs.nix | 2 + local/flake.lock | 21 ++++++ local/flake.nix | 3 + nixos/darwinModules/homebrew/default.nix | 84 +++++++++++++++++++++++ nixos/darwinProfiles/presets/homebrew.nix | 48 +++++++++++++ nixos/nixosProfiles/cloud.nix | 1 - tests/_snapshots/darwin | 35 ++++++++++ tests/darwin/expr.nix | 18 +++++ tests/darwin/format.nix | 1 + tests/nixos/__fixture/layouts.nix | 7 ++ tests/nixos/_pops.nix | 1 + 14 files changed, 234 insertions(+), 2 deletions(-) create mode 100644 nixos/darwinModules/homebrew/default.nix create mode 100644 nixos/darwinProfiles/presets/homebrew.nix create mode 100644 tests/_snapshots/darwin create mode 100644 tests/darwin/expr.nix create mode 100644 tests/darwin/format.nix diff --git a/docs/org/nixosProfiles.org b/docs/org/nixosProfiles.org index 4094253..09cd96a 100644 --- a/docs/org/nixosProfiles.org +++ b/docs/org/nixosProfiles.org @@ -24,7 +24,6 @@ inputs, }: let - inherit (inputs) dmerge; srvosCustom = (omnibus.pops.srvos.addExporters [ (POP.extendPop flops.haumea.pops.exporter ( diff --git a/lib/exporter/flakeOutputs.nix b/lib/exporter/flakeOutputs.nix index 3ad96e3..7356348 100644 --- a/lib/exporter/flakeOutputs.nix +++ b/lib/exporter/flakeOutputs.nix @@ -23,6 +23,9 @@ in nixosModules = outputs.loadNixOSModules; nixosProfiles = outputs.loadNixOSProfiles; + darwinModules = outputs.loadDarwinModules; + darwinProfiles = outputs.loadDarwinProfiles; + homeProfiles = outputs.loadHomeProfiles; homeModules = outputs.loadHomeModules; } diff --git a/lib/exporter/pops.nix b/lib/exporter/pops.nix index b4fbb9b..2f6f17f 100644 --- a/lib/exporter/pops.nix +++ b/lib/exporter/pops.nix @@ -40,6 +40,17 @@ in type = "nixosProfiles"; }; }; + + loadDarwinProfiles = self.loadNixOSProfiles.addLoadExtender { + load = { + src = inputs.self.outPath + "/nixos/darwinProfiles"; + }; + }; + + loadDarwinModules = self.loadNixOSModules.addLoadExtender { + load.src = inputs.self.outPath + "/nixos/darwinModules"; + }; + srvos = flops.haumea.pops.default.setInit { src = self.loadInputs.outputs.srvos + "/nixos"; type = "nixosProfiles"; diff --git a/local/cells/repo/configs.nix b/local/cells/repo/configs.nix index 348c61c..0a97a12 100755 --- a/local/cells/repo/configs.nix +++ b/local/cells/repo/configs.nix @@ -14,6 +14,8 @@ with inputs.std.inputs.dmerge; { "nixosProfiles" "homeProfiles" "homeModules" + "darwinModules" + "darwinProfiles" ".*." ]; }; diff --git a/local/flake.lock b/local/flake.lock index 6d4b2c8..7815048 100755 --- a/local/flake.lock +++ b/local/flake.lock @@ -83,6 +83,26 @@ "type": "github" } }, + "darwin": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1696360011, + "narHash": "sha256-HpPv27qMuPou4acXcZ8Klm7Zt0Elv9dgDvSJaomWb9Y=", + "owner": "LnL7", + "repo": "nix-darwin", + "rev": "8b6ea26d5d2e8359d06278364f41fbc4b903b28a", + "type": "github" + }, + "original": { + "owner": "LnL7", + "repo": "nix-darwin", + "type": "github" + } + }, "devshell": { "inputs": { "nixpkgs": "nixpkgs_2", @@ -686,6 +706,7 @@ "root": { "inputs": { "call-flake": "call-flake", + "darwin": "darwin", "haumea": [ "namaka", "haumea" diff --git a/local/flake.nix b/local/flake.nix index fc4d1d8..abcf42a 100755 --- a/local/flake.nix +++ b/local/flake.nix @@ -3,6 +3,9 @@ inputs.std.follows = "std-ext/std"; inputs.nixpkgs.follows = "std-ext/nixpkgs"; + inputs.darwin.url = "github:LnL7/nix-darwin"; + inputs.darwin.inputs.nixpkgs.follows = "nixpkgs"; + inputs.std-ext.url = "github:gtrunsec/std-ext"; inputs.std-ext.inputs.org-roam-book-template.follows = ""; inputs.call-flake.url = "github:divnix/call-flake"; diff --git a/nixos/darwinModules/homebrew/default.nix b/nixos/darwinModules/homebrew/default.nix new file mode 100644 index 0000000..df82688 --- /dev/null +++ b/nixos/darwinModules/homebrew/default.nix @@ -0,0 +1,84 @@ +let + casks = lib.subtractLists cfg.__profiles__.casks.removePackagesFromProfiles ( + lib.optionals cfg.__profiles__.readers [ + "koodo-reader" + "skim" + ] + ++ (lib.optionals cfg.__profiles__.shell [ "wez/wezterm/wezterm-nightly" ]) + ++ (lib.optionals cfg.__profiles__.chat [ + "telegram-desktop" + "signal-desktop" + "element-desktop" + ]) + ++ (lib.optionals cfg.__profiles__.security [ "secretive" ]) + ++ (lib.optionals cfg.__profiles__.containers [ "docker" ]) + ++ (lib.optionals cfg.__profiles__.chinese [ "squirrel" ]) + ++ (lib.optionals cfg.__profiles__.customization [ "bartender" ]) + ++ (lib.optionals cfg.__profiles__.fonts [ + "font-fantasque-sans-mono" + "font-fontawesome" + ]) + ); + brews = lib.subtractLists cfg.__profiles__.brews.removePackagesFromProfiles ( + lib.optionals cfg.__profiles__.emacs [ "emacs-plus@29" ] + ++ (lib.optionals cfg.__profiles__.fonts [ + "fontconfig" + "rxvt-unicode" + ]) + ); +in +{ + options.__profiles__ = with lib; { + default = lib.mkOption { + type = types.bool; + default = true; + description = "Whether to enable default profile"; + }; + readers = mkEnableOption (lib.mdDoc "Whether to enable readers profile"); + shell = mkEnableOption (lib.mdDoc "Whether to enable shell profile"); + chat = mkEnableOption (lib.mdDoc "Whether to enable chat profile"); + security = mkEnableOption (lib.mdDoc "Whether to enable security profile"); + containers = mkEnableOption (lib.mdDoc "Whether to enable containers profile"); + chinese = mkEnableOption (lib.mdDoc "Whether to enable chinese profile"); + fonts = mkEnableOption (lib.mdDoc "Whether to enable fonts profile"); + emacs = mkEnableOption (lib.mdDoc "Whether to enable emacs profile"); + customization = mkEnableOption ( + lib.mdDoc "Whether to enable customization profile" + ); + + casks = { + removePackagesFromProfiles = mkOption { + type = types.listOf types.str; + default = [ ]; + description = "List of packages to remove from cask profiles"; + }; + }; + brews = { + removePackagesFromProfiles = mkOption { + type = types.listOf types.str; + default = [ ]; + description = "List of packages to remove from brew profiles"; + }; + }; + }; + + config = + with lib; + mkMerge [ + { inherit casks brews; } + (mkIf cfg.__profiles__.default { + taps = [ + "homebrew/bundle" + "homebrew/core" + ]; + }) + (mkIf (cfg.casks != [ ]) { + taps = [ + "homebrew/cask" + "homebrew/cask-versions" + ]; + }) + (mkIf cfg.__profiles__.fonts { taps = [ "homebrew/cask-fonts" ]; }) + (mkIf cfg.__profiles__.emacs { taps = [ "d12frosted/emacs-plus" ]; }) + ]; +} diff --git a/nixos/darwinProfiles/presets/homebrew.nix b/nixos/darwinProfiles/presets/homebrew.nix new file mode 100644 index 0000000..7f769ce --- /dev/null +++ b/nixos/darwinProfiles/presets/homebrew.nix @@ -0,0 +1,48 @@ +{ + omnibus, + pkgs, + lib, +}: +let + inherit (pkgs.stdenv.hostPlatform) isAarch64; + brewPrefix = if isAarch64 then "/opt/homebrew" else "/usr/local"; +in +{ + imports = [ omnibus.darwinModules.homebrew ]; + + # credit: https://github.com/montchr/dotfield/blob/dd5e577d91b1af7f13909f5dc4343717a718ff02/darwin/profiles/core/default.nix + homebrew = { + enable = true; + # onActivation.cleanup = "zap"; + onActivation.upgrade = true; + # Use the nix-darwin brewfile when invoking `brew bundle` imperatively. + global.brewfile = true; + global.lockfiles = false; + __profiles__ = { + readers = true; + shell = true; + casks.removePackagesFromProfiles = [ ]; + brews.removePackagesFromProfiles = [ ]; + }; + }; + # + # + # $ brew shellenv + # export HOMEBREW_PREFIX="/opt/homebrew"; + # export HOMEBREW_CELLAR="/opt/homebrew/Cellar"; + # export HOMEBREW_REPOSITORY="/opt/homebrew"; + # export PATH="/opt/homebrew/bin:/opt/homebrew/sbin${PATH+:$PATH}"; + # export MANPATH="/opt/homebrew/share/man${MANPATH+:$MANPATH}:"; + # export INFOPATH="/opt/homebrew/share/info:${INFOPATH:-}"; + environment.systemPath = lib.mkBefore [ + "${brewPrefix}/bin" + "${brewPrefix}/sbin" + ]; + environment.variables = { + HOMEBREW_PREFIX = brewPrefix; + HOMEBREW_CELLAR = "${brewPrefix}/Cellar"; + HOMEBREW_REPOSITORY = brewPrefix; + MANPATH = "${brewPrefix}/share/man:$MANPATH"; + INFOPATH = "${brewPrefix}/share/info:$INFOPATH"; + }; +} diff --git a/nixos/nixosProfiles/cloud.nix b/nixos/nixosProfiles/cloud.nix index 5e961e0..95457ef 100644 --- a/nixos/nixosProfiles/cloud.nix +++ b/nixos/nixosProfiles/cloud.nix @@ -7,7 +7,6 @@ inputs, }: let - inherit (inputs) dmerge; srvosCustom = (omnibus.pops.srvos.addExporters [ (POP.extendPop flops.haumea.pops.exporter ( diff --git a/tests/_snapshots/darwin b/tests/_snapshots/darwin new file mode 100644 index 0000000..622ba19 --- /dev/null +++ b/tests/_snapshots/darwin @@ -0,0 +1,35 @@ +#pretty +{ + darwinFontProfile = [ + { + args = null; + brewfileLine = "cask \"koodo-reader\""; + greedy = null; + name = "koodo-reader"; + } + { + args = null; + brewfileLine = "cask \"skim\""; + greedy = null; + name = "skim"; + } + { + args = null; + brewfileLine = "cask \"wez/wezterm/wezterm-nightly\""; + greedy = null; + name = "wez/wezterm/wezterm-nightly"; + } + { + args = null; + brewfileLine = "cask \"font-fantasque-sans-mono\""; + greedy = null; + name = "font-fantasque-sans-mono"; + } + { + args = null; + brewfileLine = "cask \"font-fontawesome\""; + greedy = null; + name = "font-fontawesome"; + } + ]; +} \ No newline at end of file diff --git a/tests/darwin/expr.nix b/tests/darwin/expr.nix new file mode 100644 index 0000000..a523c3c --- /dev/null +++ b/tests/darwin/expr.nix @@ -0,0 +1,18 @@ +{ + super, + trace, + lib, + root, + omnibus, +}: +let + exporter = root.nixos.pops.layouts.default; +in +{ + darwinFontProfile = + (exporter.layouts.darwinConfiguration [ + omnibus.darwinProfiles.presets.homebrew + { homebrew.__profiles__.fonts = true; } + ]).config.homebrew.casks; +} +// lib.optionalAttrs trace { } diff --git a/tests/darwin/format.nix b/tests/darwin/format.nix new file mode 100644 index 0000000..f9e1770 --- /dev/null +++ b/tests/darwin/format.nix @@ -0,0 +1 @@ +"pretty" diff --git a/tests/nixos/__fixture/layouts.nix b/tests/nixos/__fixture/layouts.nix index 37582b2..2310f9d 100644 --- a/tests/nixos/__fixture/layouts.nix +++ b/tests/nixos/__fixture/layouts.nix @@ -52,4 +52,11 @@ in module ]; }; + + darwinConfiguration = + module: + darwin.lib.darwinSystem { + system = "aarch64-darwin"; + modules = lib.flatten [ module ]; + }; } diff --git a/tests/nixos/_pops.nix b/tests/nixos/_pops.nix index d2c1cd4..fe438f0 100644 --- a/tests/nixos/_pops.nix +++ b/tests/nixos/_pops.nix @@ -9,6 +9,7 @@ inputs = { data = root.data; nixpkgs = inputs'.nixpkgs; + darwin = inputs'.darwin; }; }; })