8.nix-darwin/tests/homebrew.nix
Emily c449918bfb homebrew: move to system activation
This adds an optional explicit `homebrew.user` option that allows users
to avoid setting `system.primaryUser`, partly as a proof of concept
of what the interfaces should look like in the future. Homebrew only
officially support one global installation, so a singleton matches
upstream’s expectations; in practice, it may be useful for us to
nest this into `users.users.*.homebrew` instead, at the expense of
being an unsupported setup if used to its full potential. Since
that would be a breaking change to the inteface anyway, I think
adding `homebrew.user` for now is acceptable. (I think one native
Apple Silicon and one Rosetta 2 Homebrew installation – under
`/opt/homebrew` and `/usr/local` respectively – may be exceptions
to this lack of upstream support, but that would be complicated to
support even with `users.users.*.homebrew`.)

I’m not entirely sure where in system activation this should
go. Probably after the user defaults and launch agents stuff, to match
the existing logic in user activation, and I lean towards doing it
as late as possible; too early and we might not have the users and
groups required to bootstrap a Homebrew installation set up, but
as Homebrew installations could be fiddly and fail, doing it in the
middle could leave a partially‐activated system.

Probably it should be done in a launch agent or something instead, but
this is my best guess as to the appropriate place for now. The downside
is that activation scripts generally won’t be able to assume that the
Homebrew prefix is populated according to the current configuration,
but they probably shouldn’t be depending on that anyway?
2025-05-16 16:29:17 +01:00

103 lines
2.8 KiB
Nix

{ 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;
homebrew.user = "test-homebrew-user";
# 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"''}
'';
}