Skip to content

Commit

Permalink
Add another example toplevel.
Browse files Browse the repository at this point in the history
This will be used to develop a `--attr` or `-A` flag for `thebacknd
run`. The goal is to mimic the same options of `nix-build` to select an
attribute to build, push to cache, and run:

    thebacknd run -A toplevels.base
    thebacknd run -A toplevels.example
  • Loading branch information
noteed committed May 13, 2024
1 parent c09d15d commit bfba7b1
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Thebacknd is a proof-of-concept to run a NixOS system as a DigitalOcean virtual
machine in a single command.

```
$ scripts/build-toplevel.sh
$ scripts/build-toplevels.sh
/nix/store/lk6igl2f0i137q36wscfrc6n9r0jn52l-nixos-system-unnamed-23.05pre-git
$ scripts/thebacknd-run /nix/store/lk6igl2f0i137q36wscfrc6n9r0jn52l-nixos-system-unnamed-23.05pre-git
```
Expand Down
21 changes: 15 additions & 6 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,35 @@ let
rustc = toolchain;
};

os = import "${toString sources.nixpkgs}/nixos/lib/eval-config.nix" {
base = import "${toString sources.nixpkgs}/nixos/lib/eval-config.nix" {
modules = [
"${toString sources.nixpkgs}/nixos/modules/virtualisation/digital-ocean-image.nix"
./machine/configuration.nix
./machines/base/configuration.nix
];
};

qemu = import "${toString sources.nixpkgs}/nixos/lib/eval-config.nix" {
modules = [
"${toString sources.nixpkgs}/nixos/modules/virtualisation/qemu-vm.nix"
./machine/configuration.nix
./machine/no-gui.nix
./machines/base/configuration.nix
./machines/base/no-gui.nix
];
};

example = import "${toString sources.nixpkgs}/nixos/lib/eval-config.nix" {
modules = [
"${toString sources.nixpkgs}/nixos/modules/virtualisation/digital-ocean-image.nix"
./machines/base/configuration.nix
./machines/example/hello.nix
];
};

in rec
{
# Build with nix-build -A <attr>
toplevel = os.config.system.build.toplevel;
image = os.config.system.build.digitalOceanImage;
toplevels.base = base.config.system.build.toplevel;
toplevels.example = example.config.system.build.toplevel;
image = base.config.system.build.digitalOceanImage;
runvm = qemu.config.system.build.vm;
binaries = naersk.buildPackage ./.;

Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions machine/scripts.nix → machines/base/scripts.nix
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
{ pkgs }:
let
current-system = pkgs.runCommandLocal "current-system" {
script = ../scripts/current-system.sh;
script = ../../scripts/current-system.sh;
nativeBuildInputs = [ pkgs.makeWrapper ];
} ''
makeWrapper $script $out/bin/current-system \
--prefix PATH : ${pkgs.lib.makeBinPath []}
'';

desired-system = pkgs.runCommandLocal "desired-system" {
script = ../scripts/desired-system.sh;
script = ../../scripts/desired-system.sh;
nativeBuildInputs = [ pkgs.makeWrapper pkgs.curl ];
} ''
makeWrapper $script $out/bin/desired-system \
--prefix PATH : ${pkgs.lib.makeBinPath []}
'';

destroy-system = pkgs.runCommandLocal "destroy-system" {
script = ../scripts/destroy-system.sh;
script = ../../scripts/destroy-system.sh;
nativeBuildInputs = [ pkgs.makeWrapper pkgs.curl pkgs.jq ];
} ''
makeWrapper $script $out/bin/destroy-system \
--prefix PATH : ${pkgs.lib.makeBinPath []}
'';

update-system = pkgs.runCommandLocal "update-system" {
script = ../scripts/update-system.sh;
script = ../../scripts/update-system.sh;
nativeBuildInputs = [ pkgs.makeWrapper ];
} ''
install -m755 $script -D $out/bin/update-system
Expand Down
7 changes: 7 additions & 0 deletions machines/example/hello.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{ config, lib, pkgs,
... }:
{
environment.systemPackages = [
pkgs.hello
];
}
18 changes: 14 additions & 4 deletions scripts/build-toplevel.sh → scripts/build-toplevels.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
#! /usr/bin/env bash

# Build, sign, and cache the toplevel and binaries.
# Build, sign, and cache the toplevels and binaries.

nix-build -A toplevel --out-link result-toplevel
nix-build -A toplevels.base --out-link result-toplevel-base
nix-build -A toplevels.example --out-link result-toplevel-example
nix-build -A binaries --out-link result-binaries

nix store sign \
--recursive \
--key-file signing-keys/cache-priv-key.pem \
$(readlink ./result-toplevel)
$(readlink ./result-toplevel-base)
nix store sign \
--recursive \
--key-file signing-keys/cache-priv-key.pem \
$(readlink ./result-toplevel-example)
nix store sign \
--recursive \
--key-file signing-keys/cache-priv-key.pem \
$(readlink ./result-binaries)

exit 0

set -a
source .env-nix-build
set +a

nix copy --to \
's3://hypered-private-store/cache?endpoint=s3.eu-central-003.backblazeb2.com' \
$(readlink ./result-toplevel)
$(readlink ./result-toplevel-base)
nix copy --to \
's3://hypered-private-store/cache?endpoint=s3.eu-central-003.backblazeb2.com' \
$(readlink ./result-toplevel-example)
nix copy --to \
's3://hypered-private-store/cache?endpoint=s3.eu-central-003.backblazeb2.com' \
$(readlink ./result-binaries)

0 comments on commit bfba7b1

Please sign in to comment.