Skip to content

Commit

Permalink
feat: add nix flake
Browse files Browse the repository at this point in the history
  • Loading branch information
getchoo authored and ryanccn committed Oct 17, 2023
1 parent 1977846 commit 2ded55d
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 0 deletions.
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@v6
- name: Setup Nix cache
uses: DeterminateSystems/magic-nix-cache-action@v2

- name: Build
run: nix build -L --accept-flake-config --fallback .#valfisk

check:
runs-on: ubuntu-latest

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

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

- name: Run checks
run: nix flake check -L --accept-flake-config --show-trace
27 changes: 27 additions & 0 deletions .github/workflows/update-lock.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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 }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@
.env
.env.*
!.env.example

# nix stuff
.direnv
result*
repl-result-out*
24 changes: 24 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
naersk,
stdenv,
darwin,
lib,
}:
naersk.buildPackage {
src = lib.cleanSource ./.;

nativeBuildInputs = lib.optionals stdenv.isDarwin [
darwin.apple_sdk_11_0.frameworks.CoreFoundation
darwin.apple_sdk_11_0.frameworks.Security
darwin.apple_sdk_11_0.frameworks.SystemConfiguration
darwin.IOKit
];

meta = with lib; {
mainProgram = "valfisk";
description = "Next generation Ryanland Discord bot, written in Rust";
homepage = "https://github.com/ryanccn/valfisk";
maintainers = with maintainers; [getchoo ryanccn];
licenses = licenses.agpl3Only;
};
}
17 changes: 17 additions & 0 deletions docker.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
lib,
dockerTools,
valfisk,
}:
dockerTools.buildImage {
name = "valfisk";
tag = "latest";

copyToRoot = [
dockerTools.caCertificates
];

config.Cmd = [
"${lib.getExe valfisk}"
];
}
87 changes: 87 additions & 0 deletions flake.lock

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

93 changes: 93 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
{
description = "Next generation Ryanland Discord bot, written in Rust";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
naersk.url = "github:nix-community/naersk";
naersk.inputs.nixpkgs.follows = "nixpkgs";
fenix.url = "github:nix-community/fenix";
fenix.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = {
self,
nixpkgs,
naersk,
fenix,
...
}: let
inherit (nixpkgs) lib;

systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];

forAllSystems = fn: lib.genAttrs systems (sys: fn nixpkgs.legacyPackages.${sys});
fromOverlay = p: lib.fix (final: self.overlays.default final p);
in {
devShells = forAllSystems (pkgs: {
default = pkgs.mkShell {
packages = with pkgs; [
rustfmt
];

inputsFrom = [self.packages.${pkgs.system}.default];
};
});

formatter = forAllSystems (pkgs: pkgs.alejandra);

# mainly for internal use. cross compiles from darwin -> linux
cross = forAllSystems (
pkgs: let
crossPkgsFor = {
x86_64-linux = pkgs.pkgsCross.gnu64;
aarch64-linux = pkgs.pkgsCross.aarch64-multiplatform;
};

toCrossPkgs' = sys: let
crossPkgs = crossPkgsFor.${sys};
toolchain = with fenix.packages.${pkgs.system};
combine [
minimal.rustc
minimal.cargo
targets.${crossPkgs.stdenv.hostPlatform.config}.minimal.rust-std
];

naersk' = naersk.lib.${pkgs.system}.override {
cargo = toolchain;
rustc = toolchain;
};

pkgs' = fromOverlay crossPkgs;
in rec {
valfisk = pkgs'.valfisk.override {naersk = naersk';};
valfisk-docker = pkgs'.valfisk-docker.override {inherit valfisk;};
};
in
lib.optionalAttrs pkgs.stdenv.isDarwin (
lib.genAttrs (builtins.attrNames crossPkgsFor) toCrossPkgs'
)
);

packages = forAllSystems (pkgs: let
pkgs' = fromOverlay pkgs;
in {
inherit (pkgs') valfisk valfisk-docker;
default = pkgs'.valfisk;
});

overlays.default = final: prev: {
valfisk = prev.callPackage ./default.nix {
naersk = final.naersk or naersk.lib.${prev.stdenv.hostPlatform.system};
};

valfisk-docker = prev.callPackage ./docker.nix {
inherit (final) valfisk;
};
};
};
}

0 comments on commit 2ded55d

Please sign in to comment.