Skip to content

Commit bc66492

Browse files
committed
implement flake support
1 parent da5d521 commit bc66492

File tree

5 files changed

+59
-11
lines changed

5 files changed

+59
-11
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ test-efi:
2323

2424
install:
2525
$(INSTALL) -D bin/nixos-shell $(DESTDIR)$(PREFIX)/bin/nixos-shell
26-
$(INSTALL) -D share/nixos-shell/nixos-shell.nix $(DESTDIR)$(PREFIX)/share/nixos-shell/nixos-shell.nix
26+
$(INSTALL) -D share/modules/nixos-shell.nix $(DESTDIR)$(PREFIX)/share/modules/nixos-shell.nix
27+
$(INSTALL) -D share/nixos-shell.nix $(DESTDIR)$(PREFIX)/share/nixos-shell.nix

bin/nixos-shell

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
set -euo pipefail
44

55
script_dir="$(dirname "$(readlink -f "$0")")"
6+
flake_uri=
7+
flake_attr=
68

79
usage() {
8-
echo "USAGE: $0 [--builders builders] [--fallback] [-I path]
10+
echo "USAGE: $0 [--builders builders] [--flake uri] [--fallback] [-I path]
911
[--impure] [--keep-going | -k] [--keep-failed | -K]
1012
[--no-net] [--option name value] [--repair]
1113
[--refresh] [--show-trace] [--verbose | -v | -vv | -vvv | -vvvv | -vvvvv]
@@ -24,6 +26,13 @@ while [[ $# -gt 0 ]]; do
2426
usage
2527
exit
2628
;;
29+
--flake)
30+
flake="$(echo "$1" | awk -F '#' '{ print $1; }')"
31+
32+
flake_uri="$(nix flake metadata --json -- "$flake" | jq -r .url)"
33+
flake_attr="$(echo "$1" | awk -F'#' '{ print $2; }')"
34+
shift
35+
;;
2736
-I|--builders)
2837
j="$1"; shift 1
2938
extraBuildFlags+=("$i" "$j")
@@ -42,7 +51,6 @@ while [[ $# -gt 0 ]]; do
4251
esac
4352
done
4453

45-
export QEMU_NIXOS_CONFIG="$(readlink -f "$nixos_config")"
4654
unset NIXOS_CONFIG
4755

4856
tempdir=$(mktemp -d)
@@ -51,8 +59,18 @@ cleanup() {
5159
}
5260
trap cleanup EXIT SIGINT SIGQUIT ERR
5361

54-
nix-build '<nixpkgs/nixos>' -A vm -k \
55-
-I "nixos-config=${script_dir}/../share/nixos-shell/nixos-shell.nix" \
62+
if [[ -z "$flake_uri" ]]; then
63+
extraBuildFlags+=(
64+
-I "nixos-config=$nixos_config"
65+
)
66+
else
67+
extraBuildFlags+=(
68+
--argstr flakeUri "$flake_uri"
69+
--argstr flakeAttr "${flake_attr:-"vm"}"
70+
)
71+
fi
72+
73+
nix-build "${script_dir}/../share/nixos-shell.nix" -A "config.system.build.vm" -k \
5674
-o "${tempdir}/result" \
5775
"${extraBuildFlags[@]}"
5876
"$@"

default.nix

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ stdenv.mkDerivation {
1111
baseNameOf path != ".direnv" &&
1212
baseNameOf path != "result"
1313
) ./.;
14-
buildInputs = [ bash ];
14+
nativeBuildInputs = [ makeWrapper ];
1515
preConfigure = ''
1616
export PREFIX=$out
1717
'';
18+
postInstall = ''
19+
wrapProgram $out/bin/nixos-shell \
20+
--prefix PATH : ${lib.makeBinPath [ jq coreutils gawk ]}
21+
'';
1822
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
{ lib, options, config, pkgs, ... }:
1+
{ lib, options, config, pkgs, modulesPath, ... }:
22

33
let
4-
nixos_config = builtins.getEnv "QEMU_NIXOS_CONFIG";
54
cfg = config.nixos-shell;
65

76
mkVMDefault = lib.mkOverride 900;
87
in {
9-
imports = lib.optional (nixos_config != "") nixos_config ++ [
10-
<nixpkgs/nixos/modules/virtualisation/qemu-vm.nix>
8+
imports = [
9+
"${toString modulesPath}/virtualisation/qemu-vm.nix"
1110
];
12-
11+
1312
options.nixos-shell = with lib; {
1413
mounts = let
1514
cache = mkOption {

share/nixos-shell.nix

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
nixpkgs ? <nixpkgs>
3+
, system ? builtins.currentSystem
4+
, configuration ? <nixos-config>
5+
6+
, flakeUri ? null
7+
, flakeAttr ? null
8+
}:
9+
let
10+
nixos-shell-module = import ./modules/nixos-shell.nix;
11+
12+
flake = builtins.getFlake flakeUri;
13+
flakeSystem = flake.outputs.packages."${system}".nixosConfigurations."${flakeAttr}" or flake.outputs.nixosConfigurations."${flakeAttr}";
14+
in
15+
if flakeUri != null then
16+
flakeSystem.override (attrs: {
17+
modules = attrs.modules ++ [ nixos-shell-module ];
18+
})
19+
else
20+
import "${toString nixpkgs}/nixos/lib/eval-config.nix" {
21+
inherit system;
22+
modules = [
23+
configuration
24+
nixos-shell-module
25+
];
26+
}

0 commit comments

Comments
 (0)