-
-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathnixos-instantiate.sh
executable file
·86 lines (73 loc) · 2.43 KB
/
nixos-instantiate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#! /usr/bin/env bash
set -euo pipefail
# Args
nix_path=$1
config=$2
config_pwd=$3
flake=$4
shift 4
command=(nix-instantiate --show-trace --expr '
{ system, configuration, hermetic ? false, flake ? false, ... }:
let
importFromFlake = { nixosConfig }:
let
flake = (import (
fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/99f1c2157fba4bfe6211a321fd0ee43199025dbf.tar.gz";
sha256 = "0x2jn3vrawwv9xp15674wjz9pixwjyj3j771izayl962zziivbx2"; }
) {
src = ./.;
}).defaultNix;
in
builtins.getAttr nixosConfig flake.nixosConfigurations;
os =
if flake
then importFromFlake { nixosConfig = configuration; }
else if hermetic
then import configuration
else import <nixpkgs/nixos> { inherit system configuration; };
in {
inherit (builtins) currentSystem;
substituters =
builtins.concatStringsSep " " os.config.nix.binaryCaches;
trusted-public-keys =
builtins.concatStringsSep " " os.config.nix.binaryCachePublicKeys;
drv_path = os.config.system.build.toplevel.drvPath;
out_path = os.config.system.build.toplevel;
}')
if readlink --version | grep -q GNU; then
readlink="readlink -f"
else
if command -v greadlink &> /dev/null; then
readlink="greadlink -f"
else
echo "Warning: symlinks not supported because readlink is non GNU" >&2
readlink="realpath"
fi
fi
if [[ -f "$config" ]]; then
config=$($readlink "$config")
command+=(--argstr configuration "$config")
else
if $flake; then
command+=(--argstr configuration "$config" --arg flake true)
else
command+=(--arg configuration "$config")
fi
fi
# add all extra CLI args as extra build arguments
command+=("$@")
# Setting the NIX_PATH
if [[ -n "$nix_path" && "$nix_path" != "-" ]]; then
export NIX_PATH=$nix_path
fi
# Changing directory
cd "$($readlink "$config_pwd")"
# Instantiate
echo "running (instantiating): ${NIX_PATH:+NIX_PATH=$NIX_PATH} ${command[*]@Q}" -A out_path >&2
"${command[@]}" -A out_path >/dev/null
# Evaluate some more details,
# relying on preceding "Instantiate" command to perform the instantiation,
# because `--eval` is required but doesn't instantiate for some reason.
echo "running (evaluating): ${NIX_PATH:+NIX_PATH=$NIX_PATH} ${command[*]@Q}" --eval --strict --json >&2
"${command[@]}" --eval --strict --json