Skip to content

Commit

Permalink
feat: add nix flake (#1)
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Cao <70191398+ryanccn@users.noreply.github.com>
  • Loading branch information
getchoo and ryanccn authored Dec 2, 2023
1 parent 7e0dcb7 commit 6addfad
Show file tree
Hide file tree
Showing 15 changed files with 506 additions and 128 deletions.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
DISCORD_TOKEN=
PAGESPEED_API_KEY=
REDIS_URL=

GUILD_ID=
ERROR_LOGS_CHANNEL=

HOST=
PORT=
11 changes: 11 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# only use flake when `nix` is present
if command -v nix &> /dev/null; then
if ! has nix_direnv_version || ! nix_direnv_version 2.2.1; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.2.1/direnvrc" "sha256-zelF0vLbEl5uaqrfIzbgNzJWGmLzCmYAkInj/LNxvKs="
fi

watch_file ./nix/dev.nix
use flake
fi

dotenv_if_exists
108 changes: 15 additions & 93 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,116 +1,38 @@
name: Docker

on:
push:
workflow_call:
pull_request:
branches: ["main"]

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

permissions:
contents: read
packages: write

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
platform:
- linux/amd64
# - linux/arm64
arch: [x86_64, aarch64]

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
if: ${{ matrix.platform != 'linux/amd64' }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Install Nix
uses: DeterminateSystems/nix-installer-action@v8

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=raw,value=latest
- name: Setup Nix cache
uses: DeterminateSystems/magic-nix-cache-action@v2

- name: Build and push by digest
uses: docker/build-push-action@v5
id: build
with:
context: .
provenance: false
labels: ${{ steps.meta.outputs.labels }}
platforms: ${{ matrix.platform }}
cache-from: type=gha
cache-to: type=gha,mode=max
outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true

- name: Export digests
- name: Build Docker image
id: image
run: |
mkdir -p /tmp/digests
digest="${{ steps.build.outputs.digest }}"
touch "/tmp/digests/${digest#sha256:}"
nix build -L .#container-${{ matrix.arch }}
[ ! -L result ] && exit 1
echo "path=$(realpath result)" >> "$GITHUB_OUTPUT"
- name: Upload digests
- name: Upload image
uses: actions/upload-artifact@v3
with:
name: digests
path: /tmp/digests/*
name: container-${{ matrix.arch }}
path: ${{ steps.image.outputs.path }}
if-no-files-found: error
retention-days: 1

push:
runs-on: ubuntu-latest
needs:
- build

steps:
- name: Download digests
uses: actions/download-artifact@v3
with:
name: digests
path: /tmp/digests

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=raw,value=latest
- name: Create manifest list and push
working-directory: /tmp/digests
run: |
docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
$(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *)
- name: Inspect image
run: |
docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}
42 changes: 42 additions & 0 deletions .github/workflows/nix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Nix

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]

runs-on: ${{ matrix.os }}

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Nix
uses: DeterminateSystems/nix-installer-action@v8
- name: Setup Nix cache
uses: DeterminateSystems/magic-nix-cache-action@v2

- name: Build valfisk
run: nix build -L --fallback

check:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Nix
uses: DeterminateSystems/nix-installer-action@v8
- name: Setup Nix cache
uses: DeterminateSystems/magic-nix-cache-action@v2

- name: Run checks
run: nix flake check -L --show-trace
28 changes: 28 additions & 0 deletions .github/workflows/update-lock.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Update flake.lock

on:
schedule:
- cron: "0 0 * * 0"
workflow_dispatch:

jobs:
update:
runs-on: ubuntu-latest

permissions:
contents: write
pull-requests: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Nix
uses: DeterminateSystems/nix-installer-action@v6

- name: Update lockfile
uses: DeterminateSystems/update-flake-lock@v20
with:
commit-msg: "chore(flake): update inputs"
pr-title: "chore(flake): update inputs"
token: ${{ github.token }}
54 changes: 54 additions & 0 deletions .github/workflows/upload.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Upload to Registry

on:
push:
branches: [main]
workflow_dispatch:

jobs:
build:
uses: ./.github/workflows/docker.yml

push:
needs: build
runs-on: ubuntu-latest

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

permissions:
packages: write

if: github.event_name == 'push'

steps:
- uses: actions/download-artifacts@v3
with:
path: images

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ github.token }}

- name: Push to Registry
env:
TAG: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
run: |
set -eux
architectures=("x86_64" "aarch64")
for arch in "${architectures[@]}"; do
docker load < images/container-"$arch"/*.tar.gz
docker tag valfisk:latest-"$arch" ${{ env.TAG }}-"$arch"
docker push ${{ env.TAG }}-"$arch"
done
docker manifest create ${{ env.TAG }} \
--amend ${{ env.TAG }}-x86_64 \
--amend ${{ env.TAG }}-aarch64
docker manifest push ${{ env.TAG }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@
# IDEs
.vscode/
.idea/

# nix stuff
.direnv
result*
repl-result-out*
35 changes: 0 additions & 35 deletions Dockerfile

This file was deleted.

Loading

0 comments on commit 6addfad

Please sign in to comment.