diff --git a/.gitignore b/.gitignore index 37e9c85..1223924 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,6 @@ dist ## Node dependencies node_modules + +# Nix +result diff --git a/backend/nix/package.nix b/backend/nix/package.nix new file mode 100644 index 0000000..b833759 --- /dev/null +++ b/backend/nix/package.nix @@ -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 "; + "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; +} diff --git a/backend/nix/rust-version.nix b/backend/nix/rust-version.nix new file mode 100644 index 0000000..07e63ae --- /dev/null +++ b/backend/nix/rust-version.nix @@ -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; +} diff --git a/backend/shell.nix b/backend/nix/shell.nix similarity index 89% rename from backend/shell.nix rename to backend/nix/shell.nix index d54105f..203a2ed 100644 --- a/backend/shell.nix +++ b/backend/nix/shell.nix @@ -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" diff --git a/flake.nix b/flake.nix index 83563c2..b8542bd 100644 --- a/flake.nix +++ b/flake.nix @@ -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 {