Add nix flake with devshell, formatting, build and checks (#773)

### Description

this nix flake is heavily based off of prior work i have done in one of
my repos. it comes with the following features:

- building sable: `nix build .#sable` or just `nix build`. creates a
`result/` dir with the built application.
- devshell: nix users now no longer need to do download and install all
the dependencies for sable by themselves. the devshell comes with all
the dependencies needed to develop sable
- checks: `nix flake check` is configured to `lint` and `test:run` the
application, in addition to checking for correct formatting
- formatting: `nix fmt` runs all the formatters pnpm would also call,
but provides a more familiar interface for nix users.
- pre-commit checks: nix users can benefit from having their changes
checked for before the commit goes through. yay :3

`.vscode/settings.json` was updated to include hints for VS Code users
using the "nix env selector" extension to auto-detect the nix dev
environment, if you have nix installed.

I had to change `oxfmt.config.ts` just a tiny bit to make it work with
the nix stuff, because `node_modules` does not exist inside the nix
build sandbox. `pnpm fmt` successfully runs, so nothing should have
broken.

`flake-lock-fix.yml` is a small workflow heavily based on
<https://docs.determinate.systems/guides/fix-hashes/>, which triggers on
pushes to `dev` which change `pnpm-lock.yaml`. The workflow uses
`determinate-nixd` to fix hashes in the `flake.nix`, then pushes the
changes to `dev` automatically.

<!-- Please read
https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before
submitting your pull request -->

<!-- Please include a summary of the change. Please also include
relevant motivation and context. List any dependencies that are required
for this change. -->

Fixes #

#### Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings

### AI disclosure:

- [ ] Partially AI assisted (clarify which code was AI assisted and
briefly explain what it does).
- [ ] Fully AI generated (explain what all the generated code does in
moderate detail).
<!-- Write any explanation required here, but do not generate the
explanation using AI!! You must prove you understand what the code in
this PR does. -->
This commit is contained in:
haz authored and GitHub committed 2026-05-08 16:47:56 +00:00
commit f07fb1b2c3
6 files changed
+384 -4

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
name: Fix flake hashes
on:
push:
branches: [dev]
paths: ['pnpm-lock.yaml']
jobs:
fix:
name: Fix flake hashes
runs-on: ubuntu-latest
permissions:
id-token: write
contents: write
steps:
- name: Checkout dev
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
ref: dev
persist-credentials: true
- name: Install Determinate Nix
uses: DeterminateSystems/determinate-nix-action@bafaa638b9d5ec0e7e3ac1a7fc80453ef1fd265f # v3.20.0
- id: check
run: nix flake check -L
continue-on-error: true
- name: Fix hash mismatches
if: steps.check.outcome == 'failure'
run: determinate-nixd fix hashes --auto-apply
- name: Commit and push if changed
if: steps.check.outcome == 'failure'
shell: bash
run: |
set -euo pipefail
if git diff --quiet; then
echo "No changes after fix hashes; nothing to commit."
exit 0
fi
git config user.name "sable-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add --update --ignore-removal .
git commit -m "chore(nix): auto-fix nix hashes"
git push origin HEAD:dev --force-with-lease
+9
View File
@@ -26,3 +26,12 @@ build.sh
# the following line was added with nvim by Shea because its annoying to clear every so often
.vscode/bookmarks.json
# Nix things
## nix build output
result
## direnv config + local state
.envrc
.direnv
## auto-generated pre-commit config
.pre-commit-config.yaml
+3 -1
View File
@@ -12,5 +12,7 @@
},
"[json]": {
"editor.defaultFormatter": "oxc.oxc-vscode"
}
},
"nixEnvSelector.nixFile": "${workspaceFolder}/flake.nix",
"nixEnvSelector.useFlakes": true
}
Generated
+142
View File
@@ -0,0 +1,142 @@
{
"nodes": {
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1767039857,
"narHash": "sha256-vNpUSpF5Nuw8xvDLj2KCwwksIbjua2LZCqhV1LNRDns=",
"owner": "NixOS",
"repo": "flake-compat",
"rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab",
"type": "github"
},
"original": {
"owner": "NixOS",
"repo": "flake-compat",
"type": "github"
}
},
"flake-parts": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": {
"lastModified": 1777988971,
"narHash": "sha256-qIoWPDs+0/8JecyYgE3gpKQxW/4bLW/gp45vow9ioCQ=",
"owner": "hercules-ci",
"repo": "flake-parts",
"rev": "0678d8986be1661af6bb555f3489f2fdfc31f6ff",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "flake-parts",
"type": "github"
}
},
"git-hooks": {
"inputs": {
"flake-compat": "flake-compat",
"gitignore": "gitignore",
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1776796298,
"narHash": "sha256-PcRvlWayisPSjd0UcRQbhG8Oqw78AcPE6x872cPRHN8=",
"owner": "cachix",
"repo": "git-hooks.nix",
"rev": "3cfd774b0a530725a077e17354fbdb87ea1c4aad",
"type": "github"
},
"original": {
"owner": "cachix",
"repo": "git-hooks.nix",
"type": "github"
}
},
"gitignore": {
"inputs": {
"nixpkgs": [
"git-hooks",
"nixpkgs"
]
},
"locked": {
"lastModified": 1709087332,
"narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=",
"owner": "hercules-ci",
"repo": "gitignore.nix",
"rev": "637db329424fd7e46cf4185293b9cc8c88c95394",
"type": "github"
},
"original": {
"owner": "hercules-ci",
"repo": "gitignore.nix",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1777954456,
"narHash": "sha256-hGdgeU2Nk87RAuZyYjyDjFL6LK7dAZN5RE9+hrDTkDU=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "549bd84d6279f9852cae6225e372cc67fb91a4c1",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs-lib": {
"locked": {
"lastModified": 1777168982,
"narHash": "sha256-GOkGPcboWE9BmGCRMLX3worL4EMnsnG8MyKmXNeYuhQ=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "f5901329dade4a6ea039af1433fb087bd9c1fe14",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"root": {
"inputs": {
"flake-parts": "flake-parts",
"git-hooks": "git-hooks",
"nixpkgs": "nixpkgs",
"treefmt-nix": "treefmt-nix"
}
},
"treefmt-nix": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1775636079,
"narHash": "sha256-pc20NRoMdiar8oPQceQT47UUZMBTiMdUuWrYu2obUP0=",
"owner": "numtide",
"repo": "treefmt-nix",
"rev": "790751ff7fd3801feeaf96d7dc416a8d581265ba",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "treefmt-nix",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
+181
View File
@@ -0,0 +1,181 @@
# SPDX-License-Identifier: AGPL-3.0
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-parts.url = "github:hercules-ci/flake-parts";
git-hooks.url = "github:cachix/git-hooks.nix";
git-hooks.inputs.nixpkgs.follows = "nixpkgs";
treefmt-nix.url = "github:numtide/treefmt-nix";
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
};
outputs =
inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
inputs.git-hooks.flakeModule
inputs.treefmt-nix.flakeModule
];
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
perSystem =
{
config,
pkgs,
lib,
...
}:
let
self = inputs.self;
packageJson = builtins.fromJSON (builtins.readFile ./package.json);
nodejs = pkgs.nodejs_24;
pnpm = pkgs.pnpm_10;
pnpmConfigHook = pkgs.pnpmConfigHook.override { inherit pnpm; };
pnpmNativeBuildInputs = [
nodejs
pnpm
pnpmConfigHook
];
mkPnpmDeps =
{
src,
version,
pnpmInstallFlags,
}:
pkgs.fetchPnpmDeps {
inherit
pnpm
src
version
pnpmInstallFlags
;
pname = "sable";
fetcherVersion = 3;
hash = "sha256-9QIBOF1d7Z086IsOAHpOayKA3uNY0e5imYQixHKFXxw=";
};
mkPnpmCheck =
name: script:
pkgs.stdenv.mkDerivation (finalAttrs: {
pname = "sable-${name}";
inherit (packageJson) version;
src = lib.cleanSource ./.;
pnpmInstallFlags = [ "--ignore-scripts" ];
pnpmDeps = mkPnpmDeps {
inherit (finalAttrs) src version pnpmInstallFlags;
};
nativeBuildInputs = pnpmNativeBuildInputs;
buildPhase = ''
runHook preBuild
pnpm run ${script}
runHook postBuild
'';
installPhase = ''
runHook preInstall
touch $out
runHook postInstall
'';
doCheck = false;
});
in
{
treefmt = {
projectRootFile = "flake.nix";
programs = {
nixfmt.enable = true;
oxfmt.enable = true;
};
settings.global.excludes = [
"dist"
"node_modules"
"pnpm-lock.yaml"
"pnpm-workspace.yaml"
"package.json"
"LICENSE"
"CHANGELOG.md"
"./changeset"
];
};
pre-commit.settings.hooks = {
treefmt = {
enable = true;
package = config.treefmt.build.wrapper;
};
};
packages.sable = pkgs.stdenv.mkDerivation (finalAttrs: {
pname = "sable";
inherit (packageJson) version;
src = lib.cleanSource ./.;
# ignoring knope for building
pnpmInstallFlags = [ "--ignore-scripts" ];
pnpmDeps = mkPnpmDeps {
inherit (finalAttrs) src version pnpmInstallFlags;
};
nativeBuildInputs = pnpmNativeBuildInputs;
env.VITE_BUILD_HASH = self.shortRev or self.dirtyShortRev or "";
env.VITE_IS_RELEASE_TAG = "false";
buildPhase = ''
runHook preBuild
pnpm run build
runHook postBuild
'';
installPhase = ''
runHook preInstall
cp -r dist $out
runHook postInstall
'';
});
packages.default = config.packages.sable;
checks = {
build = config.packages.sable;
lint = mkPnpmCheck "lint" "lint";
fmt = mkPnpmCheck "fmt" "fmt:check";
test = mkPnpmCheck "test" "test:run";
typecheck = mkPnpmCheck "typecheck" "typecheck";
knip = mkPnpmCheck "knip" "knip";
};
devShells.default = pkgs.mkShell {
packages = [
nodejs
pnpm
pkgs.corepack
pkgs.vitejs
pkgs.oxlint
pkgs.oxfmt
pkgs.knope
pkgs.typescript
pkgs.typescript-language-server
pkgs.nil
pkgs.nixd
];
shellHook = config.pre-commit.installationScript;
};
};
};
}
+3 -3
View File
@@ -1,6 +1,6 @@
import { defineConfig } from 'oxfmt';
import type { OxfmtConfig } from 'oxfmt';
export default defineConfig({
export default {
printWidth: 100,
tabWidth: 2,
singleQuote: true,
@@ -15,4 +15,4 @@ export default defineConfig({
'CHANGELOG.md',
'./changeset',
],
});
} satisfies OxfmtConfig;