1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2025-03-13 20:30:02 +00:00

Merge pull request #498 from malob/improve-homebrew-module

Add ability to configure all known Brewfile line arguments with `homebrew` module
This commit is contained in:
Domen Kožar 2022-09-06 16:35:28 +01:00 committed by GitHub
commit 3b69bf3cc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 815 additions and 133 deletions

View file

@ -1,3 +1,24 @@
2022-08-24
- Major changes to `homebrew` module
`homebrew.cleanup` was renamed to `homebrew.onActivation.cleanup`.
`homebrew.autoUpdate` was renamed to `homebrew.onActivation.autoUpdate`.
`homebrew.onActivation.upgrade` was added, and the default behavior of the
module was changed to not upgrade installed formulae and apps.
`homebrew.global.autoUpdate` was added.
`homebrew.global.noLock` was replaced with `hombrew.global.lockfiles`.
`homebrew.caskArgs` submodule was added for configuring arguments to be used
for all casks.
`homebrew.{taps,brews,casks}` were reimplemented as lists of submodules,
instead of lists of strings, with well documented options for all available
arguments for these Brewfile entry types, while preserving backwards
compatibility with the previous implementation.
2022-08-14
- nix module updated to bring it back in sync with it's NixOS counterpart
It should now be much more fiesable to share code for this module between

View file

@ -1,177 +1,722 @@
# Created by: https://github.com/malob
{ config, lib, pkgs, ... }:
{ config, lib, options, pkgs, ... }:
with lib;
let
cfg = config.homebrew;
brewfileSection = heading: type: entries: optionalString (entries != [])
"# ${heading}\n" + (concatMapStrings (name: "${type} \"${name}\"\n") entries) + "\n";
brewfileFile = pkgs.writeText "Brewfile" cfg.brewfile;
masBrewfileSection = entries: optionalString (entries != {}) (
"# Mac App Store apps\n" +
concatStringsSep "\n" (mapAttrsToList (name: id: ''mas "${name}", id: ${toString id}'') entries) +
"\n"
);
# Brewfile creation helper functions -------------------------------------------------------------
brewfile = pkgs.writeText "Brewfile" (
optionalString (cfg.extraConfig != "") ("# Extra config\n" + cfg.extraConfig) +
brewfileSection "Taps" "tap" cfg.taps +
brewfileSection "Brews" "brew" cfg.brews +
brewfileSection "Casks" "cask" cfg.casks +
masBrewfileSection cfg.masApps +
brewfileSection "Docker containers" "whalebrew" cfg.whalebrews
);
mkBrewfileSectionString = heading: entries: optionalString (entries != [ ]) ''
# ${heading}
${concatMapStringsSep "\n" (v: v.brewfileLine or v) entries}
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"
);
'';
mkBrewfileLineValueString = v:
if isInt v then toString v
else if isFloat v then strings.floatToString v
else if isBool v then boolToString v
else if isString v then ''"${v}"''
else if isAttrs v then "{ ${concatStringsSep ", " (mapAttrsToList (n: v': "${n}: ${mkBrewfileLineValueString v'}") v)} }"
else if isList v then "[${concatMapStringsSep ", " mkBrewfileLineValueString v}]"
else abort "The value: ${generators.toPretty v} is not a valid Brewfile value.";
mkBrewfileLineOptionsListString = attrs:
concatStringsSep ", " (mapAttrsToList (n: v: "${n}: ${v}") attrs);
# Option and submodule helper functions ----------------------------------------------------------
mkDocOptionLink = optionName:
''<link xlink:href="#opt-${optionName}"><option>${optionName}</option></link>'';
mkNullOrBoolOption = args: mkOption (args // {
type = types.nullOr types.bool;
default = null;
});
mkNullOrStrOption = args: mkOption (args // {
type = types.nullOr types.str;
default = null;
});
mkInternalOption = args: mkOption (args // {
visible = false;
internal = true;
readOnly = true;
});
mkProcessedSubmodConfig = attrs: mapAttrs (_: mkBrewfileLineValueString)
(filterAttrsRecursive (n: v: n != "_module" && n != "brewfileLine" && v != null) attrs);
# Submodules -------------------------------------------------------------------------------------
# Option values and descriptions of Brewfile entries are sourced/derived from:
# * `brew` manpage: https://docs.brew.sh/Manpage
# * `brew bundle` source files (at https://github.com/Homebrew/homebrew-bundle/tree/9fffe077f1a5a722ed5bd26a87ed622e8cb64e0c):
# * lib/bundle/dsl.rb
# * lib/bundle/{brew,cask,tap}_installer.rb
# * spec/bundle/{brew,cask,tap}_installer_spec.rb
onActivationOptions = { config, ... }: {
options = {
cleanup = mkOption {
type = types.enum [ "none" "uninstall" "zap" ];
default = "none";
example = "uninstall";
description = ''
This option manages what happens to formulae installed by Homebrew, that aren't present in
the Brewfile generated by this module, during <command>nix-darwin</command> system
activation.
When set to <literal>"none"</literal> (the default), formulae not present in the generated
Brewfile are left installed.
When set to <literal>"uninstall"</literal>, <command>nix-darwin</command> invokes
<command>brew bundle [install]</command> with the <command>--cleanup</command> flag. This
uninstalls all formulae not listed in generated Brewfile, i.e.,
<command>brew uninstall</command> is run for those formulae.
When set to <literal>"zap"</literal>, <command>nix-darwin</command> invokes
<command>brew bundle [install]</command> with the <command>--cleanup --zap</command>
flags. This uninstalls all formulae not listed in the generated Brewfile, and if the
formula is a cask, removes all files associated with that cask. In other words,
<command>brew uninstall --zap</command> is run for all those formulae.
If you plan on exclusively using <command>nix-darwin</command> to manage formulae
installed by Homebrew, you probably want to set this option to
<literal>"uninstall"</literal> or <literal>"zap"</literal>.
'';
};
autoUpdate = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable Homebrew to auto-update itself and all formulae during
<command>nix-darwin</command> system activation. The default is <literal>false</literal>
so that repeated invocations of <command>darwin-rebuild switch</command> are idempotent.
Note that Homebrew auto-updates when it's been more then 5 minutes since it last updated.
Although auto-updating is disabled by default during system activation, note that Homebrew
will auto-update when you manually invoke certain Homebrew commands. To modify this
behavior see ${mkDocOptionLink "homebrew.global.autoUpdate"}.
'';
};
upgrade = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable Homebrew to upgrade outdated formulae and Mac App Store apps during
<command>nix-darwin</command> system activation. The default is <literal>false</literal>
so that repeated invocations of <command>darwin-rebuild switch</command> are idempotent.
'';
};
brewBundleCmd = mkInternalOption { type = types.str; };
};
config = {
brewBundleCmd = concatStringsSep " " (
optional (!config.autoUpdate) "HOMEBREW_NO_AUTO_UPDATE=1"
++ [ "brew bundle --file='${brewfileFile}' --no-lock" ]
++ optional (!config.upgrade) "--no-upgrade"
++ optional (config.cleanup == "uninstall") "--cleanup"
++ optional (config.cleanup == "zap") "--cleanup --zap"
);
};
};
globalOptions = { config, ... }: {
options = {
brewfile = mkOption {
type = types.bool;
default = false;
description = ''
Whether to enable Homebrew to automatically use the Brewfile that this module generates in
the Nix store, when you manually invoke <command>brew bundle</command>.
Enabling this option will change the default value of
${mkDocOptionLink "homebrew.global.lockfiles"} to <literal>false</literal> since, with
this option enabled, <command>brew bundle [install]</command> will default to using the
Brewfile that this module generates in the Nix store, unless you explicitly point it at
another Brewfile using the <literal>--file</literal> flag. As a result, it will try to
write the lockfile in the Nix store, and complain that it can't (though the command will
run successfully regardless).
Implementation note: when enabled, this option sets the
<literal>HOMEBREW_BUNDLE_FILE</literal> environment variable to the path of the Brewfile
that this module generates in the Nix store, by adding it to
${mkDocOptionLink "environment.variables"}.
'';
};
autoUpdate = mkOption {
type = types.bool;
default = true;
description = ''
Whether to enable Homebrew to auto-update itself and all formulae when you manually invoke
commands like <command>brew install</command>, <command>brew upgrade</command>,
<command>brew tap</command>, and <command>brew bundle [install]</command>.
Note that Homebrew auto-updates when you manually invoke commands like the ones mentioned
above if it's been more then 5 minutes since it last updated.
You may want to consider disabling this option if you have
${mkDocOptionLink "homebrew.onActivation.upgrade"} enabled, and
${mkDocOptionLink "homebrew.onActivation.autoUpdate"} disabled, if you want to ensure that
your installed formulae will only be upgraded during <command>nix-darwin</command> system
activation, after you've explicitly run <command>brew update</command>.
Implementation note: when disabled, this option sets the
<literal>HOMEBREW_NO_AUTO_UPDATE</literal> environment variable, by adding it to
${mkDocOptionLink "environment.variables"}.
'';
};
lockfiles = mkOption {
type = types.bool;
default = !config.brewfile;
defaultText = literalExpression "!config.homebrew.global.brewfile";
description = ''
Whether to enable Homebrew to generate lockfiles when you manually invoke
<command>brew bundle [install]</command>.
This option will default to <literal>false</literal> if
${mkDocOptionLink "homebrew.global.brewfile"} is enabled since, with that option enabled,
<command>brew bundle [install]</command> will default to using the Brewfile that this
module generates in the Nix store, unless you explicitly point it at another Brewfile
using the <literal>--file</literal> flag. As a result, it will try to write the
lockfile in the Nix store, and complain that it can't (though the command will run
successfully regardless).
Implementation note: when disabled, this option sets the
<literal>HOMEBREW_BUNDLE_NO_LOCK</literal> environment variable, by adding it to
${mkDocOptionLink "environment.variables"}.
'';
};
# The `noLock` option was replaced by `lockfiles`. Due to `homebrew.global` being a submodule,
# we can't use `mkRemovedOptionModule`, so we leave this option definition here, and trigger
# and error message with an assertion below if it's set by the user.
noLock = mkOption { visible = false; default = null; };
homebrewEnvironmentVariables = mkInternalOption { type = types.attrs; };
};
config = {
homebrewEnvironmentVariables = {
HOMEBREW_BUNDLE_FILE = mkIf config.brewfile "${brewfileFile}";
HOMEBREW_NO_AUTO_UPDATE = mkIf (!config.autoUpdate) "1";
HOMEBREW_BUNDLE_NO_LOCK = mkIf (!config.lockfiles) "1";
};
};
};
tapOptions = { config, ... }: {
options = {
name = mkOption {
type = types.str;
example = "homebrew/cask-fonts";
description = ''
When <option>clone_target</option> is unspecified, this is the name of a formula
repository to tap from GitHub using HTTPS. For example, <literal>"user/repo"</literal>
will tap https://github.com/user/homebrew-repo.
'';
};
clone_target = mkNullOrStrOption {
description = ''
Use this option to tap a formula repository from anywhere, using any transport protocol
that <command>git</command> handles. When <option>clone_target</option> is specified, taps
can be cloned from places other than GitHub and using protocols other than HTTPS, e.g.,
SSH, git, HTTP, FTP(S), rsync.
'';
};
force_auto_update = mkNullOrBoolOption {
description = ''
Whether to auto-update the tap even if it is not hosted on GitHub. By default, only taps
hosted on GitHub are auto-updated (for performance reasons).
'';
};
brewfileLine = mkInternalOption { type = types.nullOr types.str; };
};
config =
let
sCfg = mkProcessedSubmodConfig config;
in
{
brewfileLine =
"tap ${sCfg.name}"
+ optionalString (sCfg ? clone_target) ", ${sCfg.clone_target}"
+ optionalString (sCfg ? force_auto_update)
", force_auto_update: ${sCfg.force_auto_update}";
};
};
# Sourced from https://docs.brew.sh/Manpage#global-cask-options
# and valid values for `HOMEBREW_CASK_OPTS`.
caskArgsOptions = { config, ... }: {
options = {
appdir = mkNullOrStrOption {
description = ''
Target location for Applications.
Homebrew's default is <filename class='directory'>/Applications</filename>.
'';
};
colorpickerdir = mkNullOrStrOption {
description = ''
Target location for Color Pickers.
Homebrew's default is <filename class='directory'>~/Library/ColorPickers</filename>.
'';
};
prefpanedir = mkNullOrStrOption {
description = ''
Target location for Preference Panes.
Homebrew's default is <filename class='directory'>~/Library/PreferencePanes</filename>.
'';
};
qlplugindir = mkNullOrStrOption {
description = ''
Target location for QuickLook Plugins.
Homebrew's default is <filename class='directory'>~/Library/QuickLook</filename>.
'';
};
mdimporterdir = mkNullOrStrOption {
description = ''
Target location for Spotlight Plugins.
Homebrew's default is <filename class='directory'>~/Library/Spotlight</filename>.
'';
};
dictionarydir = mkNullOrStrOption {
description = ''
Target location for Dictionaries.
Homebrew's default is <filename class='directory'>~/Library/Dictionaries</filename>.
'';
};
fontdir = mkNullOrStrOption {
description = ''
Target location for Fonts.
Homebrew's default is <filename class='directory'>~/Library/Fonts</filename>.
'';
};
servicedir = mkNullOrStrOption {
description = ''
Target location for Services.
Homebrew's default is <filename class='directory'>~/Library/Services</filename>.
'';
};
input_methoddir = mkNullOrStrOption {
description = ''
Target location for Input Methods.
Homebrew's default is <filename class='directory'>~/Library/Input Methods</filename>.
'';
};
internet_plugindir = mkNullOrStrOption {
description = ''
Target location for Internet Plugins.
Homebrew's default is <filename class='directory'>~/Library/Internet Plug-Ins</filename>.
'';
};
audio_unit_plugindir = mkNullOrStrOption {
description = ''
Target location for Audio Unit Plugins.
Homebrew's default is
<filename class='directory'>~/Library/Audio/Plug-Ins/Components</filename>.
'';
};
vst_plugindir = mkNullOrStrOption {
description = ''
Target location for VST Plugins.
Homebrew's default is <filename class='directory'>~/Library/Audio/Plug-Ins/VST</filename>.
'';
};
vst3_plugindir = mkNullOrStrOption {
description = ''
Target location for VST3 Plugins.
Homebrew's default is <filename class='directory'>~/Library/Audio/Plug-Ins/VST3</filename>.
'';
};
screen_saverdir = mkNullOrStrOption {
description = ''
Target location for Screen Savers.
Homebrew's default is <filename class='directory'>~/Library/Screen Savers</filename>.
'';
};
language = mkNullOrStrOption {
description = ''
Comma-separated list of language codes to prefer for cask installation. The first matching
language is used, otherwise it reverts to the casks default language. The default value
is the language of your system.
'';
example = "zh-TW";
};
require_sha = mkNullOrBoolOption {
description = ''
Whether to require cask(s) to have a checksum.
Homebrew's default is <literal>false</literal>.
'';
};
no_quarantine = mkNullOrBoolOption {
description = "Whether to disable quarantining of downloads.";
};
no_binaries = mkNullOrBoolOption {
description = "Whether to disable linking of helper executables.";
};
brewfileLine = mkInternalOption { type = types.nullOr types.str; };
};
config =
let
sCfg = mkProcessedSubmodConfig config;
in
{
brewfileLine =
if sCfg == { } then null
else "cask_args ${mkBrewfileLineOptionsListString sCfg}";
};
};
brewOptions = { config, ... }: {
options = {
name = mkOption {
type = types.str;
description = "The name of the formula to install.";
};
args = mkOption {
type = with types; nullOr (listOf str);
default = null;
description = ''
Arguments flags to pass to <command>brew install</command>. Values should not include the
leading <literal>"--"</literal>.
'';
};
conflicts_with = mkOption {
type = with types; nullOr (listOf str);
default = null;
description = ''
List of formulae that should be unlinked and their services stopped (if they are
installed).
'';
};
restart_service = mkOption {
type = with types; nullOr (either bool (enum [ "changed" ]));
default = null;
description = ''
Whether to run <command>brew services restart</command> for the formula and register it to
launch at login (or boot). If set to <literal>"changed"</literal>, the service will only
be restarted on version changes.
Homebrew's default is <literal>false</literal>.
'';
};
start_service = mkNullOrBoolOption {
description = ''
Whether to run <command>brew services start</command> for the formula and register it to
launch at login (or boot).
Homebrew's default is <literal>false</literal>.
'';
};
link = mkNullOrBoolOption {
description = ''
Whether to link the formula to the Homebrew prefix. When this option is
<literal>null</literal>, Homebrew will use it's default behavior which is to link the
formula if it's currently unlinked and not keg-only, and to unlink the formula if it's
currently linked and keg-only.
'';
};
brewfileLine = mkInternalOption { type = types.nullOr types.str; };
};
config =
let
sCfg = mkProcessedSubmodConfig config;
sCfgSubset = removeAttrs sCfg [ "name" "restart_service" ];
in
{
brewfileLine =
"brew ${sCfg.name}"
+ optionalString (sCfgSubset != { }) ", ${mkBrewfileLineOptionsListString sCfgSubset}"
# We need to handle the `restart_service` option seperately since it can be either a bool
# or `:changed` in the Brewfile.
+ optionalString (sCfg ? restart_service) (
", restart_service: " + (
if isBool config.restart_service then sCfg.restart_service
else ":${config.restart_service}"
)
);
};
};
caskOptions = { config, ... }: {
options = {
name = mkOption {
type = types.str;
description = "The name of the cask to install.";
};
args = mkOption {
type = types.nullOr (types.submodule caskArgsOptions);
default = null;
visible = "shallow"; # so that options from `homebrew.caskArgs` aren't repeated.
description = ''
Arguments passed to <command>brew install --cask</command> when installing this cask. See
${mkDocOptionLink "homebrew.caskArgs"} for the available options.
'';
};
greedy = mkNullOrBoolOption {
description = ''
Whether to always upgrade this cask regardless of whether it's unversioned or it updates
itself.
'';
};
brewfileLine = mkInternalOption { type = types.nullOr types.str; };
};
config =
let
sCfg = mkProcessedSubmodConfig config;
sCfgSubset = removeAttrs sCfg [ "name" ];
in
{
brewfileLine =
"cask ${sCfg.name}"
+ optionalString (sCfgSubset != { }) ", ${mkBrewfileLineOptionsListString sCfgSubset}";
};
};
in
{
# Interface --------------------------------------------------------------------------------------
imports = [
(mkRenamedOptionModule [ "homebrew" "autoUpdate" ] [ "homebrew" "onActivation" "autoUpdate" ])
(mkRenamedOptionModule [ "homebrew" "cleanup" ] [ "homebrew" "onActivation" "cleanup" ])
];
options.homebrew = {
enable = mkEnableOption ''
configuring your Brewfile, and installing/updating the formulas therein via
the <command>brew bundle</command> command, using <command>nix-darwin</command>.
<command>nix-darwin</command> to manage installing/updating/upgrading Homebrew taps, formulae,
and casks, as well as Mac App Store apps and Docker containers, using Homebrew Bundle.
Note that enabling this option does not install Homebrew. See the Homebrew website for
installation instructions: https://brew.sh
Note that enabling this option does not install Homebrew, see the Homebrew
<link xlink:href="https://brew.sh">website</link> for installation instructions.
Use the ${mkDocOptionLink "homebrew.brews"}, ${mkDocOptionLink "homebrew.casks"},
${mkDocOptionLink "homebrew.masApps"}, and ${mkDocOptionLink "homebrew.whalebrews"} options
to list the Homebrew formulae, casks, Mac App Store apps, and Docker containers you'd like to
install. Use the ${mkDocOptionLink "homebrew.taps"} option, to make additional formula
repositories available to Homebrew. This module uses those options (along with the
${mkDocOptionLink "homebrew.caskArgs"} options) to generate a Brewfile that
<command>nix-darwin</command> passes to the <command>brew bundle</command> command during
system activation.
The default configuration of this module prevents Homebrew Bundle from auto-updating Homebrew
and all formulae, as well as upgrading anything that's already installed, so that repeated
invocations of <command>darwin-rebuild switch</command> (without any change to the
configuration) are idempotent. You can modify this behavior using the options under
${mkDocOptionLink "homebrew.onActivation"}.
This module also provides a few options for modifying how Homebrew commands behave when
you manually invoke them, under ${mkDocOptionLink "homebrew.global"}
'';
autoUpdate = mkOption {
type = types.bool;
default = false;
description = ''
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.
'';
};
brewPrefix = mkOption {
type = types.str;
default = if pkgs.stdenv.hostPlatform.isAarch64 then "/opt/homebrew/bin" else "/usr/local/bin";
defaultText = literalExpression ''
if pkgs.stdenv.hostPlatform.isAarch64 then "/opt/homebrew/bin"
else "/usr/local/bin"
'';
description = ''
Customize path prefix where executable of <command>brew</command> is searched for.
The path prefix where the <command>brew</command> executable is located. This will be set to
the correct value based on your system's platform, and should only need to be changed if you
manually installed Homebrew in a non-standard location.
'';
};
cleanup = mkOption {
type = types.enum [ "none" "uninstall" "zap" ];
default = "none";
example = "uninstall";
onActivation = mkOption {
type = types.submodule onActivationOptions;
default = { };
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
<command>brew bundle [install]</command> with the <command>--cleanup</command> flag. This
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
<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,
<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>.
Options for configuring the behavior of the <command>brew bundle</command> command that
<command>nix-darwin</command> runs during system activation.
'';
};
global.brewfile = mkOption {
type = types.bool;
default = false;
global = mkOption {
type = types.submodule globalOptions;
default = { };
description = ''
When enabled, when you manually invoke <command>brew bundle</command>, it will automatically
use the Brewfile in the Nix store that this module generates.
Sets the <literal>HOMEBREW_BUNDLE_FILE</literal> environment variable to the path of the
Brewfile in the Nix store that this module generates, by adding it to
<option>environment.variables</option>.
'';
};
global.noLock = mkOption {
type = types.bool;
default = false;
description = ''
When enabled, lockfiles aren't generated when you manually invoke
<command>brew bundle [install]</command>. This is often desirable when
<option>homebrew.global.brewfile</option> is enabled, since
<command>brew bundle [install]</command> will try to write the lockfile in the Nix store,
and complain that it can't (though the command will run successfully regardless).
Sets the <literal>HOMEBREW_BUNDLE_NO_LOCK</literal> environment variable, by adding it to
<option>environment.variables</option>.
Options for configuring the behavior of Homebrew commands when you manually invoke them.
'';
};
taps = mkOption {
type = with types; listOf str;
default = [];
example = [ "homebrew/cask-fonts" ];
description = "Homebrew formula repositories to tap.";
type = with types; listOf (coercedTo str (name: { inherit name; }) (submodule tapOptions));
default = [ ];
example = literalExpression ''
# Adapted examples from https://github.com/Homebrew/homebrew-bundle#usage
[
# `brew tap`
"homebrew/cask"
# `brew tap` with custom Git URL and arguments
{
name = "user/tap-repo";
clone_target = "https://user@bitbucket.org/user/homebrew-tap-repo.git";
force_auto_update = true;
}
]
'';
description = ''
List of Homebrew formula repositories to tap.
Taps defined as strings, e.g., <literal>"user/repo"</literal>, are a shorthand for:
<code>{ name = "user/repo"; }</code>
'';
};
caskArgs = mkOption {
type = types.submodule caskArgsOptions;
default = { };
example = literalExpression ''
{
appdir = "~/Applications";
require_sha = true;
}
'';
description = ''
Arguments passed to <command>brew install --cask</command> for all casks listed in
${mkDocOptionLink "homebrew.casks"}.
'';
};
brews = mkOption {
type = with types; listOf str;
default = [];
example = [ "mas" ];
description = "Homebrew brews to install.";
type = with types; listOf (coercedTo str (name: { inherit name; }) (submodule brewOptions));
default = [ ];
example = literalExpression ''
# Adapted examples from https://github.com/Homebrew/homebrew-bundle#usage
[
# `brew install`
"imagemagick"
# `brew install --with-rmtp`, `brew services restart` on version changes
{
name = "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)
{
name = "mysql@5.6";
restart_service = true;
link = true;
conflicts_with = [ "mysql" ];
}
]
'';
description = ''
List of Homebrew formulae to install.
Formulae defined as strings, e.g., <literal>"imagemagick"</literal>, are a shorthand for:
<code>{ name = "imagemagick"; }</code>
'';
};
casks = mkOption {
type = with types; listOf str;
default = [];
example = [ "hammerspoon" "virtualbox" ];
description = "Homebrew casks to install.";
type = with types; listOf (coercedTo str (name: { inherit name; }) (submodule caskOptions));
default = [ ];
example = literalExpression ''
# Adapted examples from https://github.com/Homebrew/homebrew-bundle#usage
[
# `brew install --cask`
"google-chrome"
# `brew install --cask --appdir=~/my-apps/Applications`
{
name = "firefox";
args = { appdir = "~/my-apps/Applications"; };
}
# always upgrade auto-updated or unversioned cask to latest version even if already installed
{
name = "opera";
greedy = true;
}
]
'';
description = ''
List of Homebrew casks to install.
Casks defined as strings, e.g., <literal>"google-chrome"</literal>, are a shorthand for:
<code>{ name = "google-chrome"; }</code>
'';
};
masApps = mkOption {
type = with types; attrsOf ints.positive;
default = {};
example = {
"1Password" = 1107421413;
Xcode = 497799835;
};
type = types.attrsOf types.ints.positive;
default = { };
example = literalExpression ''
{
"1Password for Safari" = 1569813296;
Xcode = 497799835;
}
'';
description = ''
Applications to install from Mac App Store using <command>mas</command>.
When this option is used, <literal>"mas"</literal> is automatically added to
<option>homebrew.brews</option>.
${mkDocOptionLink "homebrew.brews"}.
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
<option>homebrew.cleanup</option> is set to <literal>"uninstall"</literal>
${mkDocOptionLink "homebrew.onActivation.cleanup"} is set to <literal>"uninstall"</literal>
or <literal>"zap"</literal> (this is currently a limitation of Homebrew Bundle).
For more information on <command>mas</command> see: https://github.com/mas-cli/mas.
For more information on <command>mas</command> see:
<link xlink:href="https://github.com/mas-cli/mas">github.com/mas-cli/mas</link>.
'';
};
whalebrews = mkOption {
type = with types; listOf str;
default = [];
default = [ ];
example = [ "whalebrew/wget" ];
description = ''
Docker images to install using <command>whalebrew</command>.
List of Docker images to install using <command>whalebrew</command>.
When this option is used, <literal>"whalebrew"</literal> is automatically added to
<option>homebrew.brews</option>.
${mkDocOptionLink "homebrew.brews"}.
For more information on <command>whalebrew</command> see:
https://github.com/whalebrew/whalebrew.
<link xlink:href="https://github.com/whalebrew/whalebrew">github.com/whalebrew/whalebrew</link>.
'';
};
@ -179,41 +724,55 @@ in
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"]
# '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"
'';
description = "Extra lines to be added verbatim to the generated Brewfile.";
description = "Extra lines to be added verbatim to the bottom of the generated Brewfile.";
};
brewfile = mkInternalOption {
type = types.str;
description = "String reprensentation of the generated Brewfile useful for debugging.";
};
};
config = {
homebrew.brews =
optional (cfg.masApps != {}) "mas" ++
optional (cfg.whalebrews != []) "whalebrew";
environment.variables = mkIf cfg.enable (
optionalAttrs cfg.global.brewfile { HOMEBREW_BUNDLE_FILE = "${brewfile}"; } //
optionalAttrs cfg.global.noLock { HOMEBREW_BUNDLE_NO_LOCK = "1"; }
);
# Implementation ---------------------------------------------------------------------------------
config = {
assertions = [
# See comment above `homebrew.global.noLock` option declaration for why this is required.
{ assertion = cfg.global.noLock == null; message = "The option `homebrew.global.noLock' was removed, use `homebrew.global.lockfiles' in it's place."; }
];
warnings = [
(mkIf (options.homebrew.autoUpdate.isDefined || options.homebrew.cleanup.isDefined) "The `homebrew' module no longer upgrades outdated formulae and apps by default during `nix-darwin' system activation. To enable upgrading, set `homebrew.onActivation.upgrade = true'.")
];
homebrew.brews =
optional (cfg.masApps != { }) "mas"
++ optional (cfg.whalebrews != [ ]) "whalebrew";
homebrew.brewfile =
"# Created by `nix-darwin`'s `homebrew` module\n\n"
+ mkBrewfileSectionString "Taps" cfg.taps
+ mkBrewfileSectionString "Arguments for all casks"
(optional (cfg.caskArgs.brewfileLine != null) cfg.caskArgs)
+ mkBrewfileSectionString "Brews" cfg.brews
+ mkBrewfileSectionString "Casks" cfg.casks
+ mkBrewfileSectionString "Mac App Store apps"
(mapAttrsToList (n: id: ''mas "${n}", id: ${toString id}'') cfg.masApps)
+ mkBrewfileSectionString "Docker containers" (map (v: ''whalebrew "${v}"'') cfg.whalebrews)
+ optionalString (cfg.extraConfig != "") ("# Extra config\n" + cfg.extraConfig);
environment.variables = mkIf cfg.enable cfg.global.homebrewEnvironmentVariables;
system.activationScripts.homebrew.text = mkIf cfg.enable ''
# Homebrew Bundle
echo >&2 "Homebrew bundle..."
if [ -f "${cfg.brewPrefix}/brew" ]; then
PATH="${cfg.brewPrefix}":$PATH ${brew-bundle-command}
PATH="${cfg.brewPrefix}":$PATH ${cfg.onActivation.brewBundleCmd}
else
echo -e "\e[1;31merror: Homebrew is not installed, skipping...\e[0m" >&2
fi

View file

@ -101,6 +101,7 @@ let
tests.autossh = makeTest ./tests/autossh.nix;
tests.checks-nix-gc = makeTest ./tests/checks-nix-gc.nix;
tests.environment-path = makeTest ./tests/environment-path.nix;
tests.homebrew = makeTest ./tests/homebrew.nix;
tests.launchd-daemons = makeTest ./tests/launchd-daemons.nix;
tests.launchd-setenv = makeTest ./tests/launchd-setenv.nix;
tests.networking-hostname = makeTest ./tests/networking-hostname.nix;

101
tests/homebrew.nix Normal file
View file

@ -0,0 +1,101 @@
{ config, lib, ... }:
let
mkTest = filter: result: ''
if ! echo "$bf" | grep -F '${filter}' | grep -F '${result}' > /dev/null; then
echo Expected:
echo '${result}'
echo Actual:
echo "$bf" | grep -F '${filter}'
exit 1
fi
'';
in
{
homebrew.enable = true;
# Examples taken from https://github.com/Homebrew/homebrew-bundle
homebrew.taps = [
"homebrew/cask"
{
name = "user/tap-repo1";
clone_target = "https://user@bitbucket.org/user/homebrew-tap-repo1.git";
}
{
name = "user/tap-repo2";
clone_target = "https://user@bitbucket.org/user/homebrew-tap-repo2.git";
force_auto_update = true;
}
];
homebrew.caskArgs = {
appdir = "~/Applications";
require_sha = true;
};
homebrew.brews = [
"imagemagick"
{
name = "denji/nginx/nginx-full";
args = [ "with-rmtp" ];
restart_service = "changed";
}
{
name = "mysql@5.6";
restart_service = true;
link = true;
conflicts_with = [ "mysql" ];
}
];
homebrew.casks = [
"google-chrome"
{
name = "firefox";
args = { appdir = "~/my-apps/Applications"; };
}
{
name = "opera";
greedy = true;
}
];
homebrew.masApps = {
"1Password for Safari" = 1569813296;
Xcode = 497799835;
};
homebrew.whalebrews = [
"whalebrew/wget"
];
test = ''
bf=${lib.escapeShellArg config.homebrew.brewfile}
echo "checking tap entries in Brewfile" >&2
${mkTest "homebrew/cask" ''tap "homebrew/cask"''}
${mkTest "user/tap-repo1" ''tap "user/tap-repo1", "https://user@bitbucket.org/user/homebrew-tap-repo1.git"''}
${mkTest "user/tap-repo2" ''tap "user/tap-repo2", "https://user@bitbucket.org/user/homebrew-tap-repo2.git", force_auto_update: true''}
echo "checking cask_args entry in Brewfile" >&2
${mkTest "cask_args" ''cask_args appdir: "~/Applications", require_sha: true''}
echo "checking brew entries in Brewfile" >&2
${mkTest "imagemagick" ''brew "imagemagick"''}
${mkTest "denji/nginx/nginx-full" ''brew "denji/nginx/nginx-full", args: ["with-rmtp"], restart_service: :changed''}
${mkTest "mysql@5.6" ''brew "mysql@5.6", conflicts_with: ["mysql"], link: true, restart_service: true''}
echo "checking cask entries in Brewfile" >&2
${mkTest "google-chrome" ''cask "google-chrome"''}
${mkTest "firefox" ''cask "firefox", args: { appdir: "~/my-apps/Applications" }''}
${mkTest "opera" ''cask "opera", greedy: true''}
echo "checking mas entries in Brewfile" >&2
${mkTest "1Password for Safari" ''mas "1Password for Safari", id: 1569813296''}
${mkTest "Xcode" ''mas "Xcode", id: 497799835''}
echo "checking whalebrew entries in Brewfile" >&2
${mkTest "whalebrew/wget" ''whalebrew "whalebrew/wget"''}
'';
}