Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: create Nix flake for development and building #122

Merged
merged 9 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .dockerignore

This file was deleted.

1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use flake
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ dist/
coverage/
.vscode
Session.vim

.direnv/
.pre-commit-config.yaml
result
simde-sws*
56 changes: 0 additions & 56 deletions Dockerfile

This file was deleted.

15 changes: 0 additions & 15 deletions docker-nginx.conf

This file was deleted.

193 changes: 193 additions & 0 deletions flake.lock

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

116 changes: 116 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# ============================================================================ #
#
# Educational computer simulator on a mission to "superscalate" the study of
# computer architecture fundamentals.
#
# ---------------------------------------------------------------------------- #
{
description = "Educational computer architecture simulator";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/release-23.11";
pre-commit-hooks = {
url = "github:cachix/pre-commit-hooks.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
pnpm2nix = {
url = "github:nzbr/pnpm2nix-nzbr";
inputs.nixpkgs.follows = "nixpkgs";
};
};

outputs =
{ self
, nixpkgs
, pre-commit-hooks
, pnpm2nix
, ...
}:
let
systems = [
"x86_64-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];

# Helper to provide system-specific attributes
forAllSystems = f:
nixpkgs.lib.genAttrs systems (system:
f rec {
inherit system;
pkgs = nixpkgs.legacyPackages.${system};
commonPackages = builtins.attrValues {
inherit (pkgs) nodejs;
inherit (pkgs.nodePackages) pnpm;
};
inherit (pnpm2nix.packages.${system}) mkPnpmPackage;
});
in
{
# Pre-commit hooks
checks = forAllSystems ({ system, pkgs, ... }: {
pre-commit-check = pre-commit-hooks.lib.${system}.run {
src = builtins.path { path = ./.; };
default_stages = [ "manual" "push" ];
hooks = {
actionlint.enable = true;
commitizen.enable = true;
nixpkgs-fmt.enable = true;
biome = {
enable = true;
package = pkgs.biome;
name = "biome";
entry = "biome check --no-errors-on-unmatched";
types_or = [ "javascript" "jsx" "ts" "tsx" "json" ];
stages = [ "pre-push" ];
};
};
};
});

# Development environment
devShells = forAllSystems ({ system, pkgs, commonPackages, ... }: {
default = pkgs.mkShell {
# Add pre-commit hooks
inherit (self.checks.${system}.pre-commit-check) shellHook;
packages = commonPackages ++ self.checks.${system}.pre-commit-check.enabledPackages;
};
});

# Package derivations
packages = forAllSystems ({ system, pkgs, mkPnpmPackage, ... }: rec {
# Build package into `dist/` dir from `pnpm-lock.yaml`
dist = mkPnpmPackage {
src = ./.;
distDir = "dist";
};

# Build binary package with `static-web-server` running SIMDE dist
sws = pkgs.pkgsStatic.writeShellScriptBin "simde-sws" ''
${pkgs.pkgsStatic.static-web-server}/bin/static-web-server
'';

# Build a Docker image that runs `simde-sws`
docker = pkgs.dockerTools.buildImage {
name = sws.name;
tag = "latest";

created = "now";

copyToRoot = pkgs.buildEnv {
name = "image-root";
paths = [ sws ];
pathsToLink = [ "/bin" ];
};

config = { Cmd = [ "/bin/simde-sws" ]; };

diskSize = 128;
buildVMMemorySize = 512;
};

default = dist;
});
};
}