Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e31187

Browse files
chore: add build targets to nix flake (coder#13186)
* chore: add build targets to nix flake Enables `nix build github:coder/coder#main`! * Fix all packages * Add back pnpm * Update flake.nix Co-authored-by: Asher <[email protected]> * Remove yarn * fmt --------- Co-authored-by: Asher <[email protected]>
1 parent 05facc9 commit 3e31187

File tree

6 files changed

+185
-24
lines changed

6 files changed

+185
-24
lines changed

.github/workflows/ci.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ jobs:
3737
k8s: ${{ steps.filter.outputs.k8s }}
3838
ci: ${{ steps.filter.outputs.ci }}
3939
db: ${{ steps.filter.outputs.db }}
40+
gomod: ${{ steps.filter.outputs.gomod }}
4041
offlinedocs-only: ${{ steps.filter.outputs.offlinedocs_count == steps.filter.outputs.all_count }}
4142
offlinedocs: ${{ steps.filter.outputs.offlinedocs }}
4243
steps:
@@ -90,6 +91,9 @@ jobs:
9091
- "scaletest/**"
9192
- "tailnet/**"
9293
- "testutil/**"
94+
gomod:
95+
- "go.mod"
96+
- "go.sum"
9397
ts:
9498
- "site/**"
9599
- "Makefile"
@@ -108,6 +112,25 @@ jobs:
108112
run: |
109113
echo "${{ toJSON(steps.filter )}}"
110114
115+
update-flake:
116+
needs: changes
117+
if: needs.changes.outputs.gomod == 'true'
118+
runs-on: ${{ github.repository_owner == 'coder' && 'buildjet-8vcpu-ubuntu-2204' || 'ubuntu-latest' }}
119+
steps:
120+
- name: Checkout
121+
uses: actions/checkout@v4
122+
with:
123+
fetch-depth: 1
124+
125+
- name: Setup Go
126+
uses: ./.github/actions/setup-go
127+
128+
- name: Update Nix Flake SRI Hash
129+
run: ./scripts/update-flake.sh
130+
131+
- name: Ensure No Changes
132+
run: git diff --exit-code
133+
111134
lint:
112135
needs: changes
113136
if: needs.changes.outputs.offlinedocs-only == 'false' || needs.changes.outputs.ci == 'true' || github.ref == 'refs/heads/main'

flake.lock

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

flake.nix

+72-22
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,29 @@
44
inputs = {
55
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
66
flake-utils.url = "github:numtide/flake-utils";
7+
pnpm2nix.url = "github:nzbr/pnpm2nix-nzbr";
78
drpc.url = "github:storj/drpc/v0.0.33";
89
};
910

10-
outputs = { self, nixpkgs, flake-utils, drpc }:
11+
outputs = { self, nixpkgs, flake-utils, drpc, pnpm2nix }:
1112
flake-utils.lib.eachDefaultSystem (system:
1213
let
1314
# Workaround for: terraform has an unfree license (‘bsl11’), refusing to evaluate.
1415
pkgs = import nixpkgs { inherit system; config.allowUnfree = true; };
15-
formatter = pkgs.nixpkgs-fmt;
1616
nodejs = pkgs.nodejs-18_x;
17-
yarn = pkgs.yarn.override { inherit nodejs; };
1817
# Check in https://search.nixos.org/packages to find new packages.
1918
# Use `nix --extra-experimental-features nix-command --extra-experimental-features flakes flake update`
2019
# to update the lock file if packages are out-of-date.
2120

2221
# From https://nixos.wiki/wiki/Google_Cloud_SDK
23-
gdk = pkgs.google-cloud-sdk.withExtraComponents ([pkgs.google-cloud-sdk.components.gke-gcloud-auth-plugin]);
22+
gdk = pkgs.google-cloud-sdk.withExtraComponents ([ pkgs.google-cloud-sdk.components.gke-gcloud-auth-plugin ]);
2423

24+
# The minimal set of packages to build Coder.
2525
devShellPackages = with pkgs; [
26+
# google-chrome is not available on OSX
27+
(if pkgs.stdenv.hostPlatform.isDarwin then null else google-chrome)
28+
# strace is not available on OSX
29+
(if pkgs.stdenv.hostPlatform.isDarwin then null else strace)
2630
bat
2731
cairo
2832
curl
@@ -31,24 +35,20 @@
3135
gcc
3236
gdk
3337
getopt
34-
git
3538
gh
39+
git
3640
gnumake
3741
gnused
3842
go_1_21
3943
go-migrate
4044
golangci-lint
41-
# google-chrome is not available on OSX
42-
(if pkgs.stdenv.hostPlatform.isDarwin then null else google-chrome)
4345
gopls
4446
gotestsum
4547
jq
4648
kubectl
4749
kubectx
4850
kubernetes-helm
4951
less
50-
# Needed for many LD system libs!
51-
util-linux
5252
mockgen
5353
nfpm
5454
nodejs
@@ -67,34 +67,84 @@
6767
shellcheck
6868
shfmt
6969
sqlc
70-
# strace is not available on OSX
71-
(if pkgs.stdenv.hostPlatform.isDarwin then null else strace)
7270
terraform
7371
typos
72+
# Needed for many LD system libs!
73+
util-linux
7474
vim
7575
wget
76-
yarn
7776
yq-go
7877
zip
7978
zsh
8079
zstd
8180
];
8281

83-
allPackages = pkgs.buildEnv {
84-
name = "all-packages";
85-
paths = devShellPackages;
82+
# buildSite packages the site directory.
83+
buildSite = pnpm2nix.packages.${system}.mkPnpmPackage {
84+
src = ./site/.;
85+
# Required for the `canvas` package!
86+
extraBuildInputs = with pkgs; [ pkgs.cairo pkgs.pango pkgs.pixman ];
87+
installInPlace = true;
88+
distDir = "out";
8689
};
90+
91+
version = "v0.0.0-nix-${self.shortRev or self.dirtyShortRev}";
92+
93+
# To make faster subsequent builds, you could extract the `.zst`
94+
# slim bundle into it's own derivation.
95+
buildFat = osArch:
96+
pkgs.buildGo121Module {
97+
name = "coder-${osArch}";
98+
# Updated with ./scripts/update-flake.nix`.
99+
# This should be updated whenever go.mod changes!
100+
vendorHash = "sha256-pTRr85MtdlsI0iYGAwLAQ3QvtrDR8rDOynYx8FDaRy0=";
101+
proxyVendor = true;
102+
src = ./.;
103+
nativeBuildInputs = with pkgs; [ getopt openssl zstd ];
104+
preBuild = ''
105+
# Replaces /usr/bin/env with an absolute path to the interpreter.
106+
patchShebangs ./scripts
107+
'';
108+
buildPhase = ''
109+
runHook preBuild
110+
111+
# Unpack the site contents.
112+
mkdir -p ./site/out
113+
cp -r ${buildSite.out}/* ./site/out
114+
115+
# Build and copy the binary!
116+
export CODER_FORCE_VERSION=${version}
117+
make -j build/coder_${osArch}
118+
'';
119+
installPhase = ''
120+
mkdir -p $out/bin
121+
cp -r ./build/coder_${osArch} $out/bin/coder
122+
'';
123+
};
87124
in
88125
{
89-
defaultPackage = formatter; # or replace it with your desired default package.
90126
devShell = pkgs.mkShell {
91-
buildInputs = devShellPackages;
92-
shellHook = ''
93-
export PLAYWRIGHT_BROWSERS_PATH=${pkgs.playwright-driver.browsers}
94-
export PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS=true
95-
'';
127+
buildInputs = devShellPackages;
128+
shellHook = ''
129+
export PLAYWRIGHT_BROWSERS_PATH=${pkgs.playwright-driver.browsers}
130+
export PLAYWRIGHT_SKIP_VALIDATE_HOST_REQUIREMENTS=true
131+
'';
132+
};
133+
packages = {
134+
all = pkgs.buildEnv {
135+
name = "all-packages";
136+
paths = devShellPackages;
137+
};
138+
site = buildSite;
139+
140+
# Copying `OS_ARCHES` from the Makefile.
141+
linux_amd64 = buildFat "linux_amd64";
142+
linux_arm64 = buildFat "linux_arm64";
143+
darwin_amd64 = buildFat "darwin_amd64";
144+
darwin_arm64 = buildFat "darwin_arm64";
145+
windows_amd64 = buildFat "windows_amd64.exe";
146+
windows_arm64 = buildFat "windows_arm64.exe";
96147
};
97-
packages.all = allPackages;
98148
}
99149
);
100150
}

scripts/lib.sh

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ SCRIPT="${BASH_SOURCE[1]:-${BASH_SOURCE[0]}}"
4343
SCRIPT_DIR="$(realpath "$(dirname "$SCRIPT")")"
4444

4545
function project_root {
46+
# Nix sets $src in derivations!
47+
[[ -n "${src:-}" ]] && echo "$src" && return
48+
4649
# Try to use `git rev-parse --show-toplevel` to find the project root.
4750
# If this directory is not a git repository, this command will fail.
4851
git rev-parse --show-toplevel 2>/dev/null && return

scripts/update-flake.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
# Updates SRI hashes for flake.nix.
3+
4+
set -eu
5+
6+
cd "$(dirname "${BASH_SOURCE[0]}")/.."
7+
8+
OUT=$(mktemp -d -t nar-hash-XXXXXX)
9+
10+
echo "Downloading Go modules..."
11+
GOPATH="$OUT" go mod download
12+
echo "Calculating SRI hash..."
13+
HASH=$(go run tailscale.com/cmd/nardump --sri "$OUT/pkg/mod/cache/download")
14+
sudo rm -rf "$OUT"
15+
16+
sed -i "s/\(vendorHash = \"\)[^\"]*/\1${HASH}/" ./flake.nix

site/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,6 @@
184184
},
185185
"engines": {
186186
"npm": ">=9.0.0 <10.0.0",
187-
"node": ">=18.0.0 <19.0.0"
187+
"node": ">=18.0.0 <21.0.0"
188188
}
189189
}

0 commit comments

Comments
 (0)