2020-12-08 02:14:48 +00:00
|
|
|
# Created by: https://github.com/malob
|
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
2020-12-17 21:03:57 +00:00
|
|
|
cfg = config.homebrew;
|
2020-12-08 02:14:48 +00:00
|
|
|
|
2020-12-17 22:42:07 +00:00
|
|
|
brewfileSection = heading: type: entries: optionalString (entries != [])
|
|
|
|
"# ${heading}\n" + (concatMapStrings (name: "${type} \"${name}\"\n") entries) + "\n";
|
|
|
|
|
|
|
|
masBrewfileSection = entries: optionalString (entries != {}) (
|
|
|
|
"# Mac App Store apps\n" +
|
|
|
|
concatStringsSep "\n" (mapAttrsToList (name: id: ''mas "${name}", id: ${toString id}'') entries) +
|
|
|
|
"\n"
|
|
|
|
);
|
2020-12-08 02:14:48 +00:00
|
|
|
|
|
|
|
brewfile = pkgs.writeText "Brewfile" (
|
2020-12-17 22:42:07 +00:00
|
|
|
brewfileSection "Taps" "tap" cfg.taps +
|
|
|
|
brewfileSection "Brews" "brew" cfg.brews +
|
|
|
|
brewfileSection "Casks" "cask" cfg.casks +
|
|
|
|
masBrewfileSection cfg.masApps +
|
2020-12-28 18:45:30 +00:00
|
|
|
brewfileSection "Docker containers" "whalebrew" cfg.whalebrews +
|
2020-12-17 22:42:07 +00:00
|
|
|
optionalString (cfg.extraConfig != "") ("# Extra config\n" + cfg.extraConfig)
|
2020-12-08 02:14:48 +00:00
|
|
|
);
|
|
|
|
|
2020-12-17 22:42:07 +00:00
|
|
|
brew-bundle-command = concatStringsSep " " (
|
|
|
|
optional (!cfg.autoUpdate) "HOMEBREW_NO_AUTO_UPDATE=1" ++
|
|
|
|
[ "brew bundle --file='${brewfile}' --no-lock" ] ++
|
|
|
|
optional (cfg.cleanup == "uninstall" || cfg.cleanup == "zap") "--cleanup" ++
|
|
|
|
optional (cfg.cleanup == "zap") "--zap"
|
|
|
|
);
|
2020-12-08 02:14:48 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
2020-12-17 21:03:57 +00:00
|
|
|
options.homebrew = {
|
2020-12-08 02:14:48 +00:00
|
|
|
enable = mkEnableOption ''
|
2020-12-08 20:22:27 +00:00
|
|
|
configuring your Brewfile, and installing/updating the formulas therein via
|
2020-12-08 02:14:48 +00:00
|
|
|
the <command>brew bundle</command> command, using <command>nix-darwin</command>.
|
|
|
|
|
2020-12-18 19:52:03 +00:00
|
|
|
When enabled, the <literal>HOMEBREW_BUNDLE_FILE</literal> and
|
|
|
|
<literal>HOMEBREW_BUNDLE_NO_LOCK</literal> environment variables are added to
|
|
|
|
<option>environment.variables</option>, so that when if/when you run
|
|
|
|
<command>brew bundle</command> yourself, it will reference the Brewfile generated by this
|
|
|
|
module in the store by default, and skip generating a lockfile (which would fail, since
|
|
|
|
lockfiles are generated in the same directory as the Brewfile).
|
|
|
|
|
2020-12-08 02:14:48 +00:00
|
|
|
Note that enabling this option does not install Homebrew. See the Homebrew website for
|
2020-12-08 20:22:27 +00:00
|
|
|
installation instructions: https://brew.sh
|
2020-12-08 02:14:48 +00:00
|
|
|
'';
|
|
|
|
|
2020-12-15 03:28:31 +00:00
|
|
|
autoUpdate = mkOption {
|
2020-12-10 19:06:34 +00:00
|
|
|
type = types.bool;
|
2020-12-15 03:28:31 +00:00
|
|
|
default = false;
|
2020-12-10 19:06:34 +00:00
|
|
|
description = ''
|
2020-12-15 03:28:31 +00:00
|
|
|
When enabled, Homebrew is allowed to auto-update during <command>nix-darwin</command>
|
|
|
|
activation. The default is <literal>false</literal> so that repeated invocations of
|
|
|
|
<command>darwin-rebuild switch</command> are idempotent.
|
2020-12-10 19:06:34 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2020-12-15 00:06:41 +00:00
|
|
|
cleanup = mkOption {
|
2020-12-10 19:06:34 +00:00
|
|
|
type = types.enum [ "none" "uninstall" "zap" ];
|
2020-12-08 20:22:27 +00:00
|
|
|
default = "none";
|
|
|
|
example = "uninstall";
|
2020-12-08 02:14:48 +00:00
|
|
|
description = ''
|
|
|
|
This option manages what happens to formulas installed by Homebrew, that aren't present in
|
|
|
|
the Brewfile generated by this module.
|
|
|
|
|
|
|
|
When set to <literal>"none"</literal> (the default), formulas not present in the generated
|
|
|
|
Brewfile are left installed.
|
|
|
|
|
|
|
|
When set to <literal>"uninstall"</literal>, <command>nix-darwin</command> invokes
|
2020-12-18 19:52:03 +00:00
|
|
|
<command>brew bundle [install]</command> with the <command>--cleanup</command> flag. This
|
2020-12-08 02:14:48 +00:00
|
|
|
uninstalls all formulas not listed in generate Brewfile, i.e.,
|
|
|
|
<command>brew uninstall</command> is run for those formulas.
|
|
|
|
|
|
|
|
When set to <literal>"zap"</literal>, <command>nix-darwin</command> invokes
|
2020-12-18 19:52:03 +00:00
|
|
|
<command>brew bundle [install]</command> with the <command>--cleanup --zap</command>
|
|
|
|
flags. This uninstalls all formulas not listed in the generated Brewfile, and if the
|
|
|
|
formula is a cask, removes all files associated with that cask. In other words,
|
2020-12-08 02:14:48 +00:00
|
|
|
<command>brew uninstall --zap</command> is run for all those formulas.
|
|
|
|
|
|
|
|
If you plan on exclusively using <command>nix-darwin</command> to manage formulas installed
|
|
|
|
by Homebrew, you probably want to set this option to <literal>"uninstall"</literal> or
|
|
|
|
<literal>"zap"</literal>.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
taps = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [];
|
|
|
|
example = [ "homebrew/cask-fonts" ];
|
2020-12-18 19:52:03 +00:00
|
|
|
description = "Homebrew formula repositories to tap.";
|
2020-12-08 02:14:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
brews = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [];
|
|
|
|
example = [ "mas" ];
|
2020-12-18 19:52:03 +00:00
|
|
|
description = "Homebrew brews to install.";
|
2020-12-08 02:14:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
casks = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [];
|
|
|
|
example = [ "hammerspoon" "virtualbox" ];
|
2020-12-18 19:52:03 +00:00
|
|
|
description = "Homebrew casks to install.";
|
2020-12-08 02:14:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
masApps = mkOption {
|
2020-12-18 19:52:03 +00:00
|
|
|
type = with types; attrsOf ints.positive;
|
2020-12-08 02:14:48 +00:00
|
|
|
default = {};
|
|
|
|
example = {
|
|
|
|
"1Password" = 1107421413;
|
|
|
|
Xcode = 497799835;
|
|
|
|
};
|
|
|
|
description = ''
|
|
|
|
Applications to install from Mac App Store using <command>mas</command>.
|
|
|
|
|
2020-12-08 20:22:27 +00:00
|
|
|
When this option is used, <literal>"mas"</literal> is automatically added to
|
2020-12-17 21:03:57 +00:00
|
|
|
<option>homebrew.brews</option>.
|
2020-12-08 02:14:48 +00:00
|
|
|
|
|
|
|
Note that you need to be signed into the Mac App Store for <command>mas</command> to
|
|
|
|
successfully install and upgrade applications, and that unfortunately apps removed from this
|
|
|
|
option will not be uninstalled automatically even if
|
2020-12-17 21:03:57 +00:00
|
|
|
<option>homebrew.cleanup</option> is set to <literal>"uninstall"</literal>
|
2020-12-08 02:14:48 +00:00
|
|
|
or <literal>"zap"</literal> (this is currently a limitation of Homebrew Bundle).
|
|
|
|
|
2020-12-18 19:52:03 +00:00
|
|
|
For more information on <command>mas</command> see: https://github.com/mas-cli/mas.
|
2020-12-08 02:14:48 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
whalebrews = mkOption {
|
|
|
|
type = with types; listOf str;
|
|
|
|
default = [];
|
|
|
|
example = [ "whalebrew/wget" ];
|
|
|
|
description = ''
|
|
|
|
Docker images to install using <command>whalebrew</command>.
|
|
|
|
|
2020-12-08 20:22:27 +00:00
|
|
|
When this option is used, <literal>"whalebrew"</literal> is automatically added to
|
2020-12-17 21:03:57 +00:00
|
|
|
<option>homebrew.brews</option>.
|
2020-12-08 02:14:48 +00:00
|
|
|
|
|
|
|
For more information on <command>whalebrew</command> see:
|
2020-12-18 19:52:03 +00:00
|
|
|
https://github.com/whalebrew/whalebrew.
|
2020-12-08 02:14:48 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
extraConfig = mkOption {
|
|
|
|
type = types.lines;
|
|
|
|
default = "";
|
|
|
|
example = ''
|
|
|
|
# 'brew tap' with custom Git URL
|
|
|
|
tap "user/tap-repo", "https://user@bitbucket.org/user/homebrew-tap-repo.git"
|
|
|
|
|
|
|
|
# set arguments for all 'brew cask install' commands
|
|
|
|
cask_args appdir: "~/Applications", require_sha: true
|
|
|
|
|
|
|
|
# 'brew install --with-rmtp', 'brew services restart' on version changes
|
|
|
|
brew "denji/nginx/nginx-full", args: ["with-rmtp"], restart_service: :changed
|
|
|
|
# 'brew install', always 'brew services restart', 'brew link', 'brew unlink mysql' (if it is installed)
|
|
|
|
brew "mysql@5.6", restart_service: true, link: true, conflicts_with: ["mysql"]
|
|
|
|
|
2020-12-08 19:42:15 +00:00
|
|
|
# 'brew cask install --appdir=~/my-apps/Applications'
|
|
|
|
cask "firefox", args: { appdir: "~/my-apps/Applications" }
|
|
|
|
# 'brew cask install' only if '/usr/libexec/java_home --failfast' fails
|
|
|
|
cask "java" unless system "/usr/libexec/java_home --failfast"
|
2020-12-08 02:14:48 +00:00
|
|
|
'';
|
|
|
|
description = "Extra lines to be added verbatim to the generated Brewfile.";
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
2020-12-17 21:03:57 +00:00
|
|
|
homebrew.brews =
|
2020-12-08 02:14:48 +00:00
|
|
|
optional (cfg.masApps != {}) "mas" ++
|
|
|
|
optional (cfg.whalebrews != []) "whalebrew";
|
|
|
|
|
2020-12-18 19:52:03 +00:00
|
|
|
environment.variables = mkIf cfg.enable {
|
|
|
|
HOMEBREW_BUNDLE_FILE = "${brewfile}";
|
|
|
|
HOMEBREW_BUNDLE_NO_LOCK = "1";
|
|
|
|
};
|
2020-12-08 02:14:48 +00:00
|
|
|
|
2020-12-17 21:03:57 +00:00
|
|
|
system.activationScripts.homebrew.text = mkIf cfg.enable ''
|
2020-12-08 02:14:48 +00:00
|
|
|
# Homebrew Bundle
|
|
|
|
echo >&2 "Homebrew bundle..."
|
2020-12-18 00:54:29 +00:00
|
|
|
if [ -f /usr/local/bin/brew ]; then
|
|
|
|
PATH=/usr/local/bin:$PATH ${brew-bundle-command}
|
|
|
|
else
|
|
|
|
echo -e "\e[1;31merror: Homebrew is not installed, skipping...\e[0m" >&2
|
|
|
|
fi
|
2020-12-08 02:14:48 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
}
|