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

add example flake and support --flake in darwin-rebuild

This commit is contained in:
Daiderd Jordan 2020-10-18 14:22:54 +02:00
parent 4a31cba84c
commit d865c6ae4c
No known key found for this signature in database
GPG key ID: D02435D05B810C96
2 changed files with 80 additions and 8 deletions

View file

@ -0,0 +1,27 @@
{
description = "Example darwin system flake";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-20.09-darwin";
darwin.url = "github:lnl7/nix-darwin/flakes";
darwin.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, darwin, nixpkgs }:
let
configuration = { pkgs, ... }: {
nix.package = pkgs.nixFlakes;
};
in
{
# Build darwin flake using:
# $ darwin-rebuild build --flake ./modules/examples#darwinConfigurations.simple.system \
# --override-input darwin .
darwinConfigurations."simple" = darwin.lib.darwinSystem {
modules = [ configuration darwin.darwinModules.simple ];
};
# Expose the package set, including overlays, for convenience.
darwinPackages = self.darwinConfigurations."simple".pkgs;
};
}

View file

@ -11,6 +11,7 @@ showSyntax() {
echo " [-Q] [{--max-jobs | -j} number] [--cores number] [--dry-run]" >&1
echo " [--keep-going] [-k] [--keep-failed] [-K] [--fallback] [--show-trace]" >&2
echo " [-I path] [--option name value] [--arg name value] [--argstr name value]" >&2
echo " [--flake flake] [--update-input input flake] [--recreate-lock-file] [--no-update-lock-file] ..." >&2
exec man darwin-rebuild
exit 1
}
@ -18,9 +19,11 @@ showSyntax() {
# Parse the command line.
origArgs=("$@")
extraBuildFlags=()
extraLockFlags=()
extraProfileFlags=()
profile=@profile@
action=
flake=
while [ $# -gt 0 ]; do
i=$1; shift 1
@ -55,6 +58,22 @@ while [ $# -gt 0 ]; do
shift 2
extraBuildFlags+=("$i" "$j" "$k")
;;
--flake)
flake=$1
shift 1
;;
--recreate-lock-file|--no-update-lock-file|--no-write-lock-file|--no-registries|--commit-lock-file)
extraLockFlags+=("$i")
;;
--update-input)
j="$1"; shift 1
extraLockFlags+=("$i" "$j")
;;
--override-input)
j="$1"; shift 1
k="$1"; shift 1
extraLockFlags+=("$i" "$j" "$k")
;;
--list-generations)
action="list"
extraProfileFlags=("$i")
@ -92,22 +111,48 @@ done
if [ -z "$action" ]; then showSyntax; fi
if ! [ "$action" = build ]; then
flakeFlags=(--experimental-features 'nix-command flakes')
if [ -n "$flake" ]; then
if [[ $flake =~ ^(.*)\#([^\#\"]*)$ ]]; then
flake="${BASH_REMATCH[1]}"
flakeAttr="${BASH_REMATCH[2]}"
fi
if [ -z "$flakeAttr" ]; then
flakeAttr=$(hostname)
flakeAttr=darwinConfigurations.${flakeAttr%.local}
fi
fi
if [ -n "$flake" ]; then
flake=$(nix "${flakeFlags[@]}" flake info --json "${extraBuildFlags[@]}" "${extraLockFlags[@]}" -- "$flake" | jq -r .url)
fi
if [ "$action" != build ] && [ -z "$flake" ]; then
extraBuildFlags+=("--no-out-link")
fi
if [ "$action" = edit ]; then
darwinConfig=$(nix-instantiate --find-file darwin-config)
exec "${EDITOR:-vi}" "$darwinConfig"
if [ -z "$flake" ]; then
exec "${EDITOR:-vi}" "$darwinConfig"
else
exec nix "${flakeFlags[@]}" edit "${extraLockFlags[@]}" -- "$flake#$flakeAttr"
fi
fi
if [ "$action" = switch -o "$action" = build -o "$action" = check ]; then
if [ "$action" = switch ] || [ "$action" = build ] || [ "$action" = check ]; then
echo "building the system configuration..." >&2
systemConfig="$(nix-build '<darwin>' "${extraBuildFlags[@]}" -A system)"
if [ -z "$flake" ]; then
systemConfig="$(nix-build '<darwin>' "${extraBuildFlags[@]}" -A system)"
else
nix "${flakeFlags[@]}" build "$flake#$flakeAttr.system" "${extraBuildFlags[@]}" "${extraLockFlags[@]}"
systemConfig=$(readlink -f result)
fi
fi
if [ "$action" = list -o "$action" = rollback ]; then
if [ "$USER" != root -a ! -w $(dirname "$profile") ]; then
if [ "$action" = list ] || [ "$action" = rollback ]; then
if [ "$USER" != root ] && [ ! -w $(dirname "$profile") ]; then
sudo nix-env -p "$profile" "${extraProfileFlags[@]}"
else
nix-env -p "$profile" "${extraProfileFlags[@]}"
@ -125,14 +170,14 @@ fi
if [ -z "$systemConfig" ]; then exit 0; fi
if [ "$action" = switch ]; then
if [ "$USER" != root -a ! -w $(dirname "$profile") ]; then
if [ "$USER" != root ] && [ ! -w $(dirname "$profile") ]; then
sudo nix-env -p "$profile" --set "$systemConfig"
else
nix-env -p "$profile" --set "$systemConfig"
fi
fi
if [ "$action" = switch -o "$action" = activate -o "$action" = rollback ]; then
if [ "$action" = switch ] || [ "$action" = activate ] || [ "$action" = rollback ]; then
"$systemConfig/activate-user"
if [ "$USER" != root ]; then