Skip to content

Commit

Permalink
clean up API clients, work on cross-builds of binaries + container im…
Browse files Browse the repository at this point in the history
…ages
  • Loading branch information
rcoder committed Oct 18, 2024
1 parent 4568c9b commit 07a845f
Show file tree
Hide file tree
Showing 26 changed files with 225 additions and 149 deletions.
79 changes: 40 additions & 39 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[workspace]

resolver = "2"
members = ["zeronsd", "central-api", "service-api", "tests"]
members = ["zeronsd", "tests", "zerotier-api"]
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM nixos/nix:latest AS builder

COPY . /tmp/build
WORKDIR /tmp/build

RUN nix \
--extra-experimental-features "nix-command flakes" \
--option filter-syscalls false \
build

RUN mkdir /tmp/nix-store-closure
RUN cp -R $(nix-store -qR result/) /tmp/nix-store-closure

FROM scratch

WORKDIR /

# Copy /nix/store
COPY --from=builder /tmp/nix-store-closure /nix/store
COPY --from=builder /tmp/build/result/bin/zeronsd /bin/zeronsd
CMD ["/bin/zeronsd"]
11 changes: 0 additions & 11 deletions central-api/Cargo.toml

This file was deleted.

3 changes: 0 additions & 3 deletions central-api/src/lib.rs

This file was deleted.

19 changes: 19 additions & 0 deletions docker.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{ pkgs, cargo-package, ... }:

pkgs.dockerTools.buildImage {
name = cargo-package.name;
tag = cargo-package.version;
created = "now";

copyToRoot = pkgs.buildEnv {
name = "zeronsd-image-root";
paths = with pkgs; [ zeronsd zerotierone dockerTools.caCertificates ];
pathsToLink = [ "/bin" "/etc" ];
};

config = {
Cmd = [ "/bin/zeronsd" ];
WorkingDir = "/var/lib/zeronsd";
Volumes = { "/var/lib/zeronsd" = {}; };
};
}
53 changes: 22 additions & 31 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,38 @@

outputs = { nixpkgs, flake-utils, rust-overlay, ... }:
let
cargo-config = builtins.fromTOML (builtins.readFile ./zeronsd/Cargo.toml);
cargo-package = (builtins.fromTOML (builtins.readFile ./zeronsd/Cargo.toml)).package;
rust-version = "1.81.0";
in {
} // flake-utils.lib.eachDefaultSystem (system:
in flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ rust-overlay.overlays.default ];
config.allowUnfree = true;
};

devInputs = with pkgs; [
rust-bin.stable.${rust-version}.complete
pkg-config
openssl
];
toml-cli
] ++ lib.optionals pkgs.stdenv.isDarwin [
pkgs.libiconv
pkgs.darwin.apple_sdk.frameworks.SystemConfiguration
] ;

zeronsd-bin = pkgs.rustPlatform.buildRustPackage rec {
inherit (cargo-config.package) name version;
inherit (cargo-package) name version;

src = ./.;
buildAndTestSubdir = "zeronsd";

nativeBuildInputs = devInputs;
buildInputs = devInputs;

cargoLock = {
lockFile = ./Cargo.lock;
};
cargoLock.lockFile = ./Cargo.lock;
};
in
{
in rec {
devShells.default = pkgs.mkShell {
buildInputs = devInputs;
nativeBuildInputs = [ pkgs.just ];
Expand All @@ -49,30 +50,20 @@
packages = {
zeronsd = zeronsd-bin;
default = zeronsd-bin;
} // builtins.listToAttrs (map (s: {
name = "container-${s}";
value = import ./docker.nix {
inherit cargo-package;

container = pkgs.dockerTools.buildImage {
inherit (cargo-config.package) name;
tag = "latest";

copyToRoot = pkgs.buildEnv {
name = "image-root";
paths = [ zeronsd-bin pkgs.dockerTools.caCertificates ];
pathsToLink = [ "/bin" "/etc" ];
};

created = "now";

runAsRoot = ''
#{pkgs.runtimeShell}
mkdir -p /var/lib/zeronsd
'';

config = {
Cmd = [ "/bin/zeronsd" ];
WorkingDir = "/var/lib/zeronsd";
pkgs = import nixpkgs {
localSystem = system;
crossSystem = s;

overlays = [ overlays.default ];
config.allowUnfree = true;
};
};
};
}) [ "x86_64-linux" "aarch64-linux" ]);

overlays = {
default = final: prev: { zeronsd = zeronsd-bin; };
Expand Down
21 changes: 16 additions & 5 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
VERSION := shell("toml get zeronsd/Cargo.toml package.version")
DOCKER_TAG := "zeronsd:" + VERSION

version:
@echo {{VERSION}}

build:
nix build

build-image:
nix build .#container
docker load < result
docker tag {{DOCKER_TAG}} zeronsd:latest

dns-ip network:
@sudo zerotier-cli -j listnetworks \
resolve network name:
nix run nixpkg#dig @$(zerotier-cli -j listnetworks \
| jq -r \
'.[] | select(.id == "{{network}}") \
| .dns.servers \
| select(.[] | test("\\d+\\.\\d+\\.\\d+\\.\\d+")) \
| last'
| last') \
{{name}}

run network config="./config.yaml" token="./.central.token": build
file {{config}}
sudo docker load < result
sudo docker run \
--net=host \
--init \
-v {{config}}:/var/lib/zeronsd/config.yaml \
-v {{token}}:/var/lib/zeronsd/central.token \
-v /var/lib/zerotier-one:/var/lib/zerotier-one \
zeronsd:latest \
zerotier/{{DOCKER_TAG}} \
zeronsd start -c /var/lib/zeronsd/config.yaml {{network}}
Loading

0 comments on commit 07a845f

Please sign in to comment.