Skip to content

Commit 37c36a1

Browse files
Lagojagcurtis
andauthored
Build and Install Devbox via Nix Flake (#2308)
## Summary This adds a flake.nix to our repo for building and distributing Devbox. This will make it easier for developers who install Devbox with Nix to use pre-releases, and keep Devbox up-to-date on their system. This changes our workflow in a few small ways: 1. Whenever `go.mod` changes, we'll need to run `devbox run update-hash` to update the vendorHash for the flake. You can also run this script when you run `go mod tidy` 2. Releasing a new version will also require us to update the version number in the flake.nix. This should be done with a commit ("Bump version to x.y") that also gets tagged with the release number. 3. For debugging purposes, the flake will add the shortened commit hash to the version string. This also helps identify if a user built the binary from a flake. 4. I've added a test to `cli-tests` that will build the flake. I've confirmed that it will fail if the vendorHash is out of date or incorrect 5. We'll periodically need to run `nix flake update` to update the flake.lock file. ## How was it tested? 1. Ran `nix build .` in the Devbox repo, confirmed that the resulting build worked 2. Tested the Github Action by pushing an invalid vendorHash --------- Signed-off-by: John Lago <[email protected]> Co-authored-by: Greg Curtis <[email protected]>
1 parent 2544163 commit 37c36a1

File tree

7 files changed

+175
-24
lines changed

7 files changed

+175
-24
lines changed

.github/workflows/cli-tests.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ jobs:
7676
- uses: actions/checkout@v4
7777
- uses: crate-ci/[email protected]
7878

79+
flake-test:
80+
name: Test Flake Build
81+
if: github.ref != 'refs/heads/main'
82+
runs-on: ubuntu-latest
83+
steps:
84+
- uses: actions/checkout@v4
85+
- uses: DeterminateSystems/nix-installer-action@main
86+
- run: nix build .
87+
- run: ./result/bin/devbox version
88+
7989
golangci-lint:
8090
strategy:
8191
matrix:

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ __pycache__/
3838
# deployment
3939
.vercel
4040
.yarn
41+
42+
# Nix
43+
vendor/
44+
result

devbox.json

+30-20
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,55 @@
11
{
2-
"name": "devbox",
2+
"name": "devbox",
33
"description": "Instant, easy, and predictable development environments",
44
"packages": {
5-
"go": "latest",
5+
"go": "latest",
66
"runx:golangci/golangci-lint": "latest",
7-
"runx:mvdan/gofumpt": "latest",
7+
"runx:mvdan/gofumpt": "latest"
88
},
99
"env": {
1010
"GOENV": "off",
11-
"PATH": "$PATH:$PWD/dist",
11+
"PATH": "$PATH:$PWD/dist"
1212
},
1313
"shell": {
1414
"init_hook": [
1515
// Remove Go environment variables that might've been inherited from the
1616
// user's environment and could affect the build.
1717
"test -z $FISH_VERSION && \\",
1818
"unset CGO_ENABLED GO111MODULE GOARCH GOFLAGS GOMOD GOOS GOROOT GOTOOLCHAIN GOWORK || \\",
19-
"set --erase CGO_ENABLED GO111MODULE GOARCH GOFLAGS GOMOD GOOS GOROOT GOTOOLCHAIN GOWORK",
19+
"set --erase CGO_ENABLED GO111MODULE GOARCH GOFLAGS GOMOD GOOS GOROOT GOTOOLCHAIN GOWORK"
2020
],
2121
"scripts": {
2222
// Build devbox for the current platform
23-
"build": "go build -o dist/devbox ./cmd/devbox",
23+
"build": "go build -o dist/devbox ./cmd/devbox",
2424
"build-darwin-amd64": "GOOS=darwin GOARCH=amd64 go build -o dist/devbox-darwin-amd64 ./cmd/devbox",
2525
"build-darwin-arm64": "GOOS=darwin GOARCH=arm64 go build -o dist/devbox-darwin-arm64 ./cmd/devbox",
26-
"build-linux-amd64": "GOOS=linux GOARCH=amd64 go build -o dist/devbox-linux-amd64 ./cmd/devbox",
27-
"build-linux-arm64": "GOOS=linux GOARCH=arm64 go build -o dist/devbox-linux-arm64 ./cmd/devbox",
26+
"build-linux-amd64": "GOOS=linux GOARCH=amd64 go build -o dist/devbox-linux-amd64 ./cmd/devbox",
27+
"build-linux-arm64": "GOOS=linux GOARCH=arm64 go build -o dist/devbox-linux-arm64 ./cmd/devbox",
2828
"build-all": [
2929
"devbox run build-darwin-amd64",
3030
"devbox run build-darwin-arm64",
3131
"devbox run build-linux-amd64",
32-
"devbox run build-linux-arm64",
32+
"devbox run build-linux-arm64"
3333
],
3434
// Open VSCode
35-
"code": "code .",
36-
"lint": "golangci-lint run --timeout 5m && scripts/gofumpt.sh",
37-
"fmt": "scripts/gofumpt.sh",
38-
"test": "go test -race -cover ./...",
35+
"code": "code .",
36+
"lint": "golangci-lint run --timeout 5m && scripts/gofumpt.sh",
37+
"fmt": "scripts/gofumpt.sh",
38+
"test": "go test -race -cover ./...",
3939
"test-projects-only": "DEVBOX_RUN_PROJECT_TESTS=1 go test -v -timeout ${DEVBOX_GOLANG_TEST_TIMEOUT:-30m} ./... -run \"TestExamples|TestScriptsWithProjects\"",
40-
"update-examples": "devbox run build && go run testscripts/testrunner/updater/main.go",
41-
"tidy": "go mod tidy",
42-
40+
"update-examples": "devbox run build && go run testscripts/testrunner/updater/main.go",
41+
// Updates the Flake's vendorHash: First run `go mod vendor` to vendor
42+
// the dependencies, then hash the vendor directory with Nix.
43+
// The hash is saved to the `vendor-hash` file, which is then
44+
// read into the Nix Flake.
45+
"update-hash": [
46+
// realpath to work-around nix hash not liking symlinks
47+
"vendor=$(realpath $(mktemp -d))",
48+
"trap \"rm -rf $vendor\" EXIT",
49+
"go mod vendor -o $vendor",
50+
"nix hash path $vendor >vendor-hash",
51+
],
52+
"tidy": ["go mod tidy", "devbox run update-hash"],
4353
// docker-testscripts runs the testscripts with Docker to exercise
4454
// Linux-specific tests. It invokes the test binary directly, so any extra
4555
// test runner flags must have their "-test." prefix.
@@ -58,8 +68,8 @@
5868
"GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go test -c -o testscripts-linux-amd64",
5969
"GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go test -c -o testscripts-linux-arm64",
6070
"image=$(docker build --quiet --tag devbox-testscripts-ubuntu:noble --platform linux/amd64 .)",
61-
"docker run --rm --mount type=volume,src=devbox-testscripts-amd64,dst=/nix --platform linux/amd64 -e DEVBOX_RUN_FAILING_TESTS -e DEVBOX_RUN_PROJECT_TESTS -e DEVBOX_DEBUG $image \"$@\"",
62-
],
63-
},
64-
},
71+
"docker run --rm --mount type=volume,src=devbox-testscripts-amd64,dst=/nix --platform linux/amd64 -e DEVBOX_RUN_FAILING_TESTS -e DEVBOX_RUN_PROJECT_TESTS -e DEVBOX_DEBUG $image \"$@\""
72+
]
73+
}
74+
}
6575
}

flake.lock

+61
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
description = "Instant, easy, predictable shells and containers";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
};
8+
9+
outputs = { self, nixpkgs, flake-utils }:
10+
flake-utils.lib.eachDefaultSystem (system:
11+
let
12+
pkgs = nixpkgs.legacyPackages.${system};
13+
14+
lastTag = "0.13.2";
15+
16+
# Add the commit to the version string, in case someone builds from main
17+
getVersion = pkgs.lib.trivial.pipe self [
18+
(x: "${lastTag}")
19+
(x: if (self ? revCount)
20+
then "${x}-${self.shortRev}"
21+
else "${x}-${self.dirtyShortRev}")
22+
];
23+
24+
# Run `devbox run update-flake` to update the vendorHash
25+
vendorHash = if builtins.pathExists ./vendor-hash
26+
then builtins.readFile ./vendor-hash
27+
else "";
28+
29+
buildGoModule = pkgs.buildGo123Module;
30+
31+
in
32+
{
33+
inherit self;
34+
packages.default = buildGoModule rec {
35+
pname = "devbox";
36+
version = getVersion;
37+
38+
src = ./.;
39+
40+
inherit vendorHash;
41+
42+
ldflags = [
43+
"-s"
44+
"-w"
45+
"-X go.jetpack.io/devbox/internal/build.Version=${version}"
46+
];
47+
48+
# Disable tests if they require network access or are integration tests
49+
doCheck = false;
50+
51+
nativeBuildInputs = [ pkgs.installShellFiles ];
52+
53+
postInstall = ''
54+
installShellCompletion --cmd devbox \
55+
--bash <($out/bin/devbox completion bash) \
56+
--fish <($out/bin/devbox completion fish) \
57+
--zsh <($out/bin/devbox completion zsh)
58+
'';
59+
60+
meta = with pkgs.lib; {
61+
description = "Instant, easy, and predictable development environments";
62+
homepage = "https://www.jetify.com/devbox";
63+
license = licenses.asl20;
64+
maintainers = with maintainers; [ lagoja ];
65+
};
66+
};
67+
}
68+
);
69+
}

go.sum

-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor-hash

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sha256-rwmNzYzmZqNcNVV4GgqCVLT6ofIkblVCMJHLGwlhcGw=

0 commit comments

Comments
 (0)