added clap to configure port and interfaces, and fixed nixos module

This commit is contained in:
2026-03-26 19:33:30 +01:00
parent 81686d6fb8
commit 26b12e9dca
2 changed files with 132 additions and 115 deletions

220
flake.nix
View File

@@ -1,114 +1,114 @@
{ {
description = "Quick rust website template"; description = "Quick rust website template";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs =
{ self, nixpkgs, ... }:
let
supportedSystems = [
"x86_64-linux"
# "aarch64-linux"
# "x86_64-darwin"
# "aarch64-darwin"
];
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
in
{
packages = forAllSystems (
system:
let
pkgs = import nixpkgs { inherit system; };
in
{
default = pkgs.rustPlatform.buildRustPackage {
pname = "quick-rust-website";
version = "0.1.0";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [
pkgs.openssl
# pkgs.sqlite
];
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include";
};
}
);
devShells = forAllSystems (
system:
let
pkgs = import nixpkgs { inherit system; };
in
{
default = pkgs.mkShell {
packages = [
pkgs.rustc
pkgs.cargo
pkgs.rust-analyzer
pkgs.pkg-config
pkgs.openssl
];
env = {
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
OPENSSL_DIR = "${pkgs.openssl.dev}";
};
};
}
);
nixosModules.default =
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.slimes-server;
in
{
options.services.slimes-server = {
enable = lib.mkEnableOption "Quick rust website template";
port = lib.mkOption {
type = lib.types.port;
default = 9003;
};
# databasePath = lib.mkOption {
# type = lib.types.str;
# default = "/var/lib/quick-rust-website/database.db";
# };
};
config = lib.mkIf cfg.enable {
systemd.services.slimes-server = {
description = "Quick rust website template";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${
self.packages.${pkgs.system}.default
}/bin/quick-rust-website --database-url ${cfg.databasePath} --port ${toString cfg.port}";
Restart = "on-failure";
StateDirectory = "quick-rust-website";
DynamicUser = true;
ProtectSystem = "strict";
ProtectHome = true;
NoNewPrivileges = true;
};
};
};
};
}; };
outputs =
{ self, nixpkgs, ... }:
let
supportedSystems = [
"x86_64-linux"
# "aarch64-linux"
# "x86_64-darwin"
# "aarch64-darwin"
];
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
in
{
packages = forAllSystems (
system:
let
pkgs = import nixpkgs { inherit system; };
in
{
default = pkgs.rustPlatform.buildRustPackage {
pname = "quick-rust-website";
version = "0.1.0";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
nativeBuildInputs = [ pkgs.pkg-config ];
buildInputs = [
pkgs.openssl
# pkgs.sqlite
];
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
OPENSSL_LIB_DIR = "${pkgs.openssl.out}/lib";
OPENSSL_INCLUDE_DIR = "${pkgs.openssl.dev}/include";
};
}
);
devShells = forAllSystems (
system:
let
pkgs = import nixpkgs { inherit system; };
in
{
default = pkgs.mkShell {
packages = [
pkgs.rustc
pkgs.cargo
pkgs.rust-analyzer
pkgs.pkg-config
pkgs.openssl
];
env = {
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig";
OPENSSL_DIR = "${pkgs.openssl.dev}";
};
};
}
);
nixosModules.default =
{
config,
lib,
pkgs,
...
}:
let
cfg = config.services.quick-rust-website;
in
{
options.services.quick-rust-website = {
enable = lib.mkEnableOption "Quick rust website template";
port = lib.mkOption {
type = lib.types.port;
default = 9003;
};
# databasePath = lib.mkOption {
# type = lib.types.str;
# default = "/var/lib/quick-rust-website/database.db";
# };
};
config = lib.mkIf cfg.enable {
systemd.services.quick-rust-website = {
description = "Quick rust website template";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
ExecStart = "${
self.packages.${pkgs.system}.default
}/bin/quick-rust-website --port ${toString cfg.port} --host";
Restart = "on-failure";
StateDirectory = "quick-rust-website";
DynamicUser = true;
ProtectSystem = "strict";
ProtectHome = true;
NoNewPrivileges = true;
};
};
};
};
};
} }

View File

@@ -1,4 +1,5 @@
use axum::{Router, routing::get}; use axum::{Router, routing::get};
use clap::Parser;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::sync::Arc; use std::sync::Arc;
use tera::Tera; use tera::Tera;
@@ -20,12 +21,30 @@ lazy_static! {
}; };
} }
#[derive(clap::Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct Cli {
/// Port to serve on
#[arg(short, long, default_value = "8000")]
port: String,
/// Whether to listen on 0.0.0.0
#[arg(short, long)]
host: bool,
/// Verbose mode
#[arg(short, long)]
verbose: bool,
}
struct AppState { struct AppState {
app_name: String, app_name: String,
} }
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
tracing_subscriber::fmt() tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO) .with_max_level(tracing::Level::INFO)
.init(); .init();
@@ -39,12 +58,10 @@ async fn main() -> anyhow::Result<()> {
.route("/showcase", get(showcase_handler)) .route("/showcase", get(showcase_handler))
.with_state(shared_state); .with_state(shared_state);
let host = false; let addr = if cli.host {
let port = "8000"; format!("0.0.0.0:{}", cli.port)
let addr = if host {
format!("0.0.0.0:{}", port)
} else { } else {
format!("127.0.0.1:{}", port) format!("127.0.0.1:{}", cli.port)
}; };
let listener = tokio::net::TcpListener::bind(&addr).await?; let listener = tokio::net::TcpListener::bind(&addr).await?;