diff --git a/default.nix b/default.nix index 3039de2..6170de7 100644 --- a/default.nix +++ b/default.nix @@ -1,15 +1,16 @@ -{pkgs ? import {}}: let - manifest = (pkgs.lib.importTOML ./Cargo.toml).package; - rustDeps = pkgs.callPackage ./Cargo.nix {}; - packageEntry = rustDeps.workspaceMembers.${manifest.name}; - deps = packageEntry.build.cargoDeps or null; -in - pkgs.rustPlatform.buildRustPackage { - pname = manifest.name; - version = manifest.version; - cargoDeps = deps; - src = pkgs.lib.cleanSource ./.; - cargoLock.lockFile = ./Cargo.lock; - nativeBuildInputs = [pkgs.openssl pkgs.pkg-config]; - buildInputs = [pkgs.openssl]; - } +{ pkgs ? import { } }: +pkgs.rustPlatform.buildRustPackage { + pname = "imphnen-backend"; + version = (pkgs.lib.importTOML ./imphnen-backend/Cargo.toml).package.version; + src = pkgs.lib.cleanSource ./.; + cargoLock.lockFile = ./Cargo.lock; + cargoBuildFlags = [ + "--package" + "imphnen-backend" + "--bin" + "api" + ]; + nativeBuildInputs = [ pkgs.pkg-config ]; + buildInputs = [ pkgs.openssl ]; + doCheck = false; +} diff --git a/flake.nix b/flake.nix index 33a871a..443eaf8 100644 --- a/flake.nix +++ b/flake.nix @@ -1,41 +1,47 @@ -{ - description = "IMPHNEN Backend Service Nix Flake"; - - inputs = { - nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; - }; - - outputs = - { - self, - nixpkgs, - }: - let - supportedSystems = [ - "x86_64-linux" - "x86_64-darwin" - "aarch64-darwin" - "aarch64-linux" - ]; - pkgsFor = - system: - import nixpkgs { - inherit system; - config = { - allowUnfree = true; - }; - }; - forAllSystems = nixpkgs.lib.genAttrs supportedSystems; - in - { - packages = forAllSystems (system: { - default = (pkgsFor system).callPackage ./default.nix { }; - }); - devShells = forAllSystems (system: { - default = (pkgsFor system).callPackage ./shell.nix { }; - }); - dockerImages = forAllSystems (system: { - tryOutApi = (pkgsFor system).callPackage ./docker.nix { }; - }); - }; -} +{ + description = "IMPHNEN Backend Service Nix Flake"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + }; + + outputs = + { + self, + nixpkgs, + }: + let + supportedSystems = [ + "x86_64-linux" + "x86_64-darwin" + "aarch64-darwin" + "aarch64-linux" + ]; + pkgsFor = + system: + import nixpkgs { + inherit system; + config.allowUnfree = true; + }; + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + in + { + packages = forAllSystems (system: { + default = (pkgsFor system).callPackage ./default.nix { }; + }); + + overlays.default = final: _prev: { + imphnen-backend = final.callPackage ./default.nix { }; + }; + + nixosModules.backend = ./nixos-module.nix; + + devShells = forAllSystems (system: { + default = (pkgsFor system).callPackage ./shell.nix { }; + }); + + dockerImages = forAllSystems (system: { + tryOutApi = (pkgsFor system).callPackage ./docker.nix { }; + }); + }; +} diff --git a/nixos-module.nix b/nixos-module.nix new file mode 100644 index 0000000..29913da --- /dev/null +++ b/nixos-module.nix @@ -0,0 +1,48 @@ +{ config, lib, pkgs, ... }: +let + cfg = config.services.imphnen-backend; +in +{ + options.services.imphnen-backend = { + enable = lib.mkEnableOption "IMPHNEN backend service"; + + port = lib.mkOption { + type = lib.types.port; + default = 8081; + description = "Port the backend HTTP server listens on."; + }; + + environmentFile = lib.mkOption { + type = lib.types.path; + description = "Path to environment file with secrets (DATABASE_URL, JWT keys, etc)."; + }; + + openFirewall = lib.mkOption { + type = lib.types.bool; + default = false; + }; + }; + + config = lib.mkIf cfg.enable { + systemd.services.imphnen-backend = { + description = "IMPHNEN Backend Service"; + wantedBy = [ "multi-user.target" ]; + after = [ + "network.target" + "postgresql.service" + ]; + serviceConfig = { + ExecStart = "${pkgs.imphnen-backend}/bin/api"; + EnvironmentFile = cfg.environmentFile; + Environment = [ "PORT=${toString cfg.port}" ]; + DynamicUser = true; + Restart = "on-failure"; + RestartSec = "5s"; + StandardOutput = "journal"; + StandardError = "journal"; + }; + }; + + networking.firewall.allowedTCPPorts = lib.mkIf cfg.openFirewall [ cfg.port ]; + }; +}