1
0
Fork 0
mirror of https://github.com/LnL7/nix-darwin.git synced 2025-04-08 10:09:38 +00:00

Merge pull request from LnL7/flakes

flakes
This commit is contained in:
Daiderd Jordan 2020-10-21 18:46:20 +02:00 committed by GitHub
commit 67da67bb7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 271 additions and 12 deletions

View file

@ -30,3 +30,22 @@ jobs:
- run: |
nix-shell -A uninstaller
nix-shell -A uninstaller.check
install-flake:
runs-on: macos-10.15
timeout-minutes: 10
steps:
- uses: actions/checkout@v2
- uses: cachix/install-nix-action@v8
- run: |
nix-channel --update
echo "FIXME workaround channel permission error"
nix-shell -A installer
echo "TODO installed doesn't support flakes, handle multi-user install"
- run: |
nix-shell -p nixFlakes --run 'nix --experimental-features "nix-command flakes" registry add darwin $PWD'
nix-shell -p nixFlakes --run 'nix --experimental-features "nix-command flakes" build ./modules/examples#darwinConfigurations.simple.system'
- run: |
./result/sw/bin/darwin-rebuild switch --flake ./modules/examples#simple
- run: |
. /etc/static/bashrc
darwin-rebuild build --flake ./modules/examples#simple

View file

@ -61,6 +61,50 @@ Check out [modules/examples](https://github.com/LnL7/nix-darwin/tree/master/modu
}
```
## Flakes (experimental)
There is also preliminary support for building your configuration using a flake. This
is mostly based on the flake support that was added to NixOS.
A minimal example of using an existing configuration.nix:
```nix
{
description = "John's darwin system";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-20.09-darwin";
darwin.url = "github:lnl7/nix-darwin/master";
darwin.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, darwin, nixpkgs }: {
darwinConfigurations."Johns-MacBook" = darwin.lib.darwinSystem {
modules = [ ./configuration.nix ];
};
};
}
```
Inputs from the flake can also be passed to `darwinSystem`, these inputs are then
accessible as an argument, similar to pkgs and lib inside the configuration.
```nix
darwin.lib.darwinSystem {
modules = [ ... ];
inputs = { inherit darwin dotfiles nixpkgs; };
}
```
Since the installer doesn't work with flakes out of the box yet nix-darwin will need to
be to be bootstrapped using the installer or manually. Afterwards the flake based
configuration can be built. The `hostname(1)` of your system will be used to decide
which darwin configuration is applied if it's not specified explicitly in the flake ref.
```sh
darwin-rebuild switch --flake ~/.config/darwin
```
## Manual Install
```bash
@ -114,7 +158,7 @@ $
```
```
$ darwin-option services.activate-system.enable ~/src/nix-darwin
$ darwin-option services.activate-system.enable
Value:
true

41
eval-config.nix Normal file
View file

@ -0,0 +1,41 @@
{ lib, system ? builtins.currentSystem or "x86_64-darwin" }:
{ modules
, inputs
, baseModules ? import ./modules/module-list.nix
, specialArgs ? {}
}@args:
let
inputsModule = {
_file = ./eval-config.nix;
config = {
_module.args.inputs = inputs;
};
};
pkgsModule = { config, inputs, ... }: {
_file = ./eval-config.nix;
config = {
_module.args.pkgs = import inputs.nixpkgs config.nixpkgs;
nixpkgs.system = system;
};
};
eval = lib.evalModules (builtins.removeAttrs args ["inputs"] // {
modules = modules ++ [ inputsModule pkgsModule ] ++ baseModules;
args = { inherit baseModules modules; };
specialArgs = { modulesPath = builtins.toString ./modules; } // specialArgs;
});
# Was moved in nixpkgs #82751, so both need to be handled here until 20.03 is deprecated.
# https://github.com/NixOS/nixpkgs/commits/dcdd232939232d04c1132b4cc242dd3dac44be8c
_module = eval._module or eval.config._module;
in
{
inherit (_module.args) pkgs;
inherit (eval) options config;
system = eval.config.system.build.toplevel;
}

25
flake.lock generated Normal file
View file

@ -0,0 +1,25 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1602411953,
"narHash": "sha256-gbupmxRpoQZqL5NBQCJN2GI5G7XDEHHHYKhVwEj5+Ps=",
"owner": "LnL7",
"repo": "nixpkgs",
"rev": "f780534ea2d0c12e62607ff254b6b45f46653f7a",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

40
flake.nix Normal file
View file

@ -0,0 +1,40 @@
{
# WARNING this is very much still experimental.
description = "A collection of darwin modules";
outputs = { self, nixpkgs }:
let
versionsModule = { lib, ... }: {
_file = ./flake.nix;
config = {
system.darwinVersionSuffix = ".${nixpkgs.shortRev or "dirty"}";
system.darwinRevision = lib.mkIf (self ? rev) self.rev;
system.nixpkgsVersionSuffix = ".${lib.substring 0 8 (nixpkgs.lastModifiedDate or nixpkgs.lastModified or "19700101")}.${nixpkgs.shortRev or "dirty"}";
system.nixpkgsRevision = lib.mkIf (nixpkgs ? rev) nixpkgs.rev;
};
};
in
{
lib = {
# TODO handle multiple architectures.
evalConfig = import ./eval-config.nix { inherit (nixpkgs) lib; };
darwinSystem = { modules, inputs ? {}, ... }@args: self.lib.evalConfig (args // {
inputs = { inherit nixpkgs; darwin = self; } // inputs;
modules = modules ++ [ versionsModule ];
});
};
darwinModules.lnl = import ./modules/examples/lnl.nix;
darwinModules.simple = import ./modules/examples/simple.nix;
darwinModules.ofborg = import ./modules/examples/ofborg.nix;
darwinModules.hydra = import ./modules/examples/hydra.nix;
checks.x86_64-darwin.simple = (self.lib.darwinSystem {
modules = [ self.darwinModules.simple ];
}).system;
};
}

View file

@ -0,0 +1,30 @@
{
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;
# FIXME: for github actions, this shouldn't be in the example.
services.nix-daemon.enable = true;
};
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

@ -59,6 +59,12 @@ in
description = "The full darwin version (e.g. <literal>darwin4.master</literal>).";
};
system.darwinVersionSuffix = mkOption {
internal = true;
type = types.str;
description = "The short darwin version suffix (e.g. <literal>.2abdb5a</literal>).";
};
system.darwinRevision = mkOption {
internal = true;
type = types.str;
@ -78,6 +84,12 @@ in
description = "The full nixpkgs version (e.g. <literal>16.03.1160.f2d4ee1</literal>).";
};
system.nixpkgsVersionSuffix = mkOption {
internal = true;
type = types.str;
description = "The short nixpkgs version suffix (e.g. <literal>.1160.f2d4ee1</literal>).";
};
system.nixpkgsRevision = mkOption {
internal = true;
type = types.str;
@ -89,12 +101,14 @@ in
# These defaults are set here rather than up there so that
# changing them would not rebuild the manual
system.darwinLabel = "${cfg.nixpkgsVersion}+${cfg.darwinVersion}";
system.darwinVersion = "darwin" + toString cfg.stateVersion + "." + darwin.shortRev;
system.darwinLabel = mkDefault "${cfg.nixpkgsVersion}+${cfg.darwinVersion}";
system.darwinVersion = mkDefault "darwin${toString cfg.stateVersion}${cfg.darwinVersionSuffix}";
system.darwinVersionSuffix = mkDefault ".${darwin.shortRev}";
system.darwinRevision = mkIf (darwin ? rev) (mkDefault darwin.rev);
system.nixpkgsVersion = mkDefault (cfg.nixpkgsRelease + nixpkgsSuffix);
system.nixpkgsVersion = mkDefault "${cfg.nixpkgsRelease}${cfg.nixpkgsVersionSuffix}";
system.nixpkgsRelease = mkDefault (fileContents releaseFile);
system.nixpkgsVersionSuffix = mkDefault nixpkgsSuffix;
system.nixpkgsRevision = mkIf (nixpkgs ? rev) (mkDefault nixpkgs.rev);
assertions = [ { assertion = cfg.stateVersion <= defaultStateVersion; message = "system.stateVersion = ${toString cfg.stateVersion}; is not a valid value"; } ];

View file

@ -11,6 +11,8 @@ 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] [--impure] [--recreate-lock-file]"
echo " [--no-update-lock-file] ..." >&2
exec man darwin-rebuild
exit 1
}
@ -18,9 +20,11 @@ showSyntax() {
# Parse the command line.
origArgs=("$@")
extraBuildFlags=()
extraLockFlags=()
extraProfileFlags=()
profile=@profile@
action=
flake=
while [ $# -gt 0 ]; do
i=$1; shift 1
@ -55,6 +59,22 @@ while [ $# -gt 0 ]; do
shift 2
extraBuildFlags+=("$i" "$j" "$k")
;;
--flake)
flake=$1
shift 1
;;
-L|-vL|--print-build-logs|--impure|--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 +112,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)
fi
flakeAttr=darwinConfigurations.${flakeAttr%.local}
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 +171,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