feat: build backend with Nix

This commit is contained in:
Lucien Cartier-Tilet 2025-11-05 01:25:55 +01:00
parent 9d0cfd1795
commit 9b5d6d87ed
5 changed files with 69 additions and 7 deletions

3
.gitignore vendored
View File

@ -31,3 +31,6 @@ dist
## Node dependencies
node_modules
# Nix
result

55
backend/nix/package.nix Normal file
View File

@ -0,0 +1,55 @@
{
rust-overlay,
inputs,
system,
...
}: let
rust = import ./rust-version.nix { inherit rust-overlay inputs system; };
pkgs = rust.pkgs;
rustPlatform = pkgs.makeRustPlatform {
cargo = rust.version;
rustc = rust.version;
};
cargoToml = builtins.fromTOML (builtins.readFile ../Cargo.toml);
name = cargoToml.package.name;
version = cargoToml.package.version;
rustBuild = rustPlatform.buildRustPackage {
pname = name;
inherit version;
src = ../.;
cargoLock.lockFile = ../Cargo.lock;
};
makeDockerImage = tag:
pkgs.dockerTools.buildLayeredImage {
name = "phundrak/${name}";
inherit tag;
created = "now";
config = {
Entrypoint = ["${rustBuild}/bin/${name}"];
Env = [
"SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
];
ExposedPorts = {
"3100/tcp" = {};
};
Labels = {
"org.opencontainers.image.title" = name;
"org.opencontainers.image.version" = version;
"org.opencontainers.image.description" = "REST API backend for phundrak.com";
"org.opencontainers.image.authors" = "Lucien Cartier-Tilet <lucien@phundrak.com>";
"org.opencontainers.image.licenses" = "AGPL-3.0-only";
"org.opencontainers.image.source" = "https://labs.phundrak.com/phundrak/phundrak.com";
"org.opencontainers.image.url" = "https://labs.phundrak.com/phundrak/phundrak.com";
"org.opencontainers.image.documentation" = "https://labs.phundrak.com/phundrak/phundrak.com";
"org.opencontainers.image.vendor" = "Phundrak";
};
};
contents = [rustBuild pkgs.cacert];
};
dockerImageLatest = makeDockerImage "latest";
dockerImageVersioned = makeDockerImage version;
in {
backend = rustBuild;
backendDocker = dockerImageVersioned;
backendDockerLatest = dockerImageLatest;
}

View File

@ -0,0 +1,6 @@
{rust-overlay, inputs, system, ...}: let
overlays = [(import rust-overlay)];
in rec {
pkgs = import inputs.nixpkgs {inherit system overlays;};
version = pkgs.rust-bin.stable.latest.default;
}

View File

@ -6,9 +6,7 @@
rust-overlay,
...
}: let
overlays = [(import rust-overlay)];
rustPkgs = import inputs.nixpkgs {inherit system overlays;};
rustVersion = rustPkgs.rust-bin.stable.latest.default;
rustPlatform = import ./rust-version.nix { inherit rust-overlay inputs system; };
in
inputs.devenv.lib.mkShell {
inherit inputs pkgs;
@ -20,8 +18,8 @@ in
pkgs.lib.mkIf (devenvRootFileContent != "") devenvRootFileContent;
}
{
packages = with rustPkgs; [
(rustVersion.override {
packages = with rustPlatform.pkgs; [
(rustPlatform.version.override {
extensions = [
"clippy"
"rust-src"

View File

@ -33,12 +33,12 @@
forEachSystem = nixpkgs.lib.genAttrs (import systems);
in {
formatter = forEachSystem (system: alejandra.defaultPackage.${system});
packages = forEachSystem (system: import ./backend/nix/package.nix { inherit rust-overlay inputs system; });
devShells = forEachSystem (
system: let
pkgs = nixpkgs.legacyPackages.${system};
in {
backend = import ./backend/shell.nix {
backend = import ./backend/nix/shell.nix {
inherit inputs pkgs system self rust-overlay;
};
frontend = import ./frontend/shell.nix {