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

Skip to content

Commit e6c4da3

Browse files
committed
chore: add build targets to nix flake
Enables `nix build github:coder/coder#main`!
1 parent 13dd526 commit e6c4da3

File tree

6 files changed

+178
-19
lines changed

6 files changed

+178
-19
lines changed

.github/workflows/ci.yaml

Lines changed: 23 additions & 0 deletions
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

Lines changed: 70 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,55 @@
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; };
1516
formatter = pkgs.nixpkgs-fmt;
1617
nodejs = pkgs.nodejs-18_x;
17-
yarn = pkgs.yarn.override { inherit nodejs; };
1818
# Check in https://search.nixos.org/packages to find new packages.
1919
# Use `nix --extra-experimental-features nix-command --extra-experimental-features flakes flake update`
2020
# to update the lock file if packages are out-of-date.
2121

2222
# From https://nixos.wiki/wiki/Google_Cloud_SDK
2323
gdk = pkgs.google-cloud-sdk.withExtraComponents ([pkgs.google-cloud-sdk.components.gke-gcloud-auth-plugin]);
2424

25-
devShellPackages = with pkgs; [
25+
# The minimal set of packages to build Coder.
26+
devShellPackages = with pkgs; buildPackages ++ [
27+
# google-chrome is not available on OSX
28+
(if pkgs.stdenv.hostPlatform.isDarwin then null else google-chrome)
29+
# strace is not available on OSX
30+
(if pkgs.stdenv.hostPlatform.isDarwin then null else strace)
2631
bat
2732
cairo
33+
coreutils-full
2834
curl
2935
delve
3036
drpc.defaultPackage.${system}
3137
gcc
3238
gdk
3339
getopt
34-
git
3540
gh
41+
git
3642
gnumake
3743
gnused
3844
go_1_21
3945
go-migrate
4046
golangci-lint
41-
# google-chrome is not available on OSX
42-
(if pkgs.stdenv.hostPlatform.isDarwin then null else google-chrome)
4347
gopls
4448
gotestsum
4549
jq
4650
kubectl
4751
kubectx
4852
kubernetes-helm
4953
less
50-
# Needed for many LD system libs!
51-
util-linux
5254
mockgen
5355
nfpm
54-
nodejs
55-
nodejs.pkgs.pnpm
5656
openssh
5757
openssl
5858
pango
@@ -67,10 +67,10 @@
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
7676
yarn
@@ -80,21 +80,68 @@
8080
zstd
8181
];
8282

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

scripts/lib.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ SCRIPT="${BASH_SOURCE[1]:-${BASH_SOURCE[0]}}"
4343
SCRIPT_DIR="$(realpath "$(dirname "$SCRIPT")")"
4444

4545
function project_root {
46+
# If NIX_BUILD_TOP is set, we are in a Nix build and the project root is the
47+
# top of the build directory.
48+
[[ -n "${NIX_BUILD_TOP:-}" ]] && echo "$src" && return
49+
4650
# Try to use `git rev-parse --show-toplevel` to find the project root.
4751
# If this directory is not a git repository, this command will fail.
4852
git rev-parse --show-toplevel 2>/dev/null && return

scripts/update-flake.sh

Lines changed: 16 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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)