add nix way
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
};
|
||||
|
||||
outputs =
|
||||
{ nixpkgs, ... }:
|
||||
{ self, nixpkgs, ... }:
|
||||
let
|
||||
systems = [
|
||||
"aarch64-darwin"
|
||||
@@ -48,6 +48,38 @@
|
||||
exec docker-compose up "$@"
|
||||
'';
|
||||
};
|
||||
nativeRun = pkgs.writeShellApplication {
|
||||
name = "csbi-native-run";
|
||||
runtimeInputs = [
|
||||
pkgs.bun
|
||||
pkgs.coreutils
|
||||
];
|
||||
text = ''
|
||||
env_file="''${CSBIE_ENV_FILE:-$PWD/.env}"
|
||||
data_dir="''${CSBIE_DATA_DIR:-$PWD/data}"
|
||||
|
||||
if [ ! -f "$env_file" ]; then
|
||||
echo "CSBIE_ENV_FILE does not exist: $env_file" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$data_dir"
|
||||
|
||||
work_dir="$(mktemp -d)"
|
||||
trap 'rm -rf "$work_dir"' EXIT
|
||||
|
||||
source_dir="$work_dir/source"
|
||||
mkdir -p "$source_dir"
|
||||
cp -R --no-preserve=mode,ownership "${self}/." "$source_dir"
|
||||
|
||||
cd "$source_dir"
|
||||
bun install --frozen-lockfile --linker hoisted
|
||||
bun --filter @repo/csbie build
|
||||
|
||||
export CSBIE_DATABASE_PATH="''${CSBIE_DATABASE_PATH:-$data_dir/csbie.sqlite}"
|
||||
exec bun --env-file="$env_file" apps/csbie/dist/index.js "$@"
|
||||
'';
|
||||
};
|
||||
in
|
||||
{
|
||||
default = {
|
||||
@@ -61,6 +93,13 @@
|
||||
program = "${composeUp}/bin/csbi-compose-up";
|
||||
meta.description = "Run cSBI with docker-compose";
|
||||
};
|
||||
|
||||
csbie = {
|
||||
type = "app";
|
||||
program = "${nativeRun}/bin/csbi-native-run";
|
||||
meta.description = "Build and run cSBI directly with Bun";
|
||||
};
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
@@ -68,19 +107,61 @@
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.services.csbie;
|
||||
nativeRun = pkgs.writeShellApplication {
|
||||
name = "csbi-native-run";
|
||||
runtimeInputs = [
|
||||
pkgs.bun
|
||||
pkgs.coreutils
|
||||
];
|
||||
text = ''
|
||||
env_file="''${CSBIE_ENV_FILE:-$PWD/.env}"
|
||||
data_dir="''${CSBIE_DATA_DIR:-$PWD/data}"
|
||||
|
||||
if [ ! -f "$env_file" ]; then
|
||||
echo "CSBIE_ENV_FILE does not exist: $env_file" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "$data_dir"
|
||||
|
||||
work_dir="$(mktemp -d)"
|
||||
trap 'rm -rf "$work_dir"' EXIT
|
||||
|
||||
source_dir="$work_dir/source"
|
||||
mkdir -p "$source_dir"
|
||||
cp -R --no-preserve=mode,ownership "${self}/." "$source_dir"
|
||||
|
||||
cd "$source_dir"
|
||||
bun install --frozen-lockfile --linker hoisted
|
||||
bun --filter @repo/csbie build
|
||||
|
||||
export CSBIE_DATABASE_PATH="''${CSBIE_DATABASE_PATH:-$data_dir/csbie.sqlite}"
|
||||
exec bun --env-file="$env_file" apps/csbie/dist/index.js "$@"
|
||||
'';
|
||||
};
|
||||
in
|
||||
{
|
||||
options.services.csbie = {
|
||||
enable = lib.mkEnableOption "cSBI";
|
||||
|
||||
runtime = lib.mkOption {
|
||||
type = lib.types.enum [
|
||||
"docker"
|
||||
"native"
|
||||
];
|
||||
default = "docker";
|
||||
description = "Runtime backend.";
|
||||
};
|
||||
|
||||
image = lib.mkOption {
|
||||
type = lib.types.str;
|
||||
default = "git.yutakobayashi.com/nakasyou/csbi:latest";
|
||||
description = "Container image to run.";
|
||||
description = "Container image to run when runtime is docker.";
|
||||
};
|
||||
|
||||
envFile = lib.mkOption {
|
||||
@@ -101,26 +182,57 @@
|
||||
};
|
||||
};
|
||||
|
||||
config = lib.mkIf cfg.enable {
|
||||
virtualisation.oci-containers.backend = lib.mkDefault "docker";
|
||||
config = lib.mkIf cfg.enable (lib.mkMerge [
|
||||
{
|
||||
systemd.tmpfiles.rules = [
|
||||
"d ${cfg.dataDir} 0750 root root -"
|
||||
];
|
||||
}
|
||||
|
||||
systemd.tmpfiles.rules = [
|
||||
"d ${cfg.dataDir} 0750 root root -"
|
||||
];
|
||||
(lib.mkIf (cfg.runtime == "docker") {
|
||||
virtualisation.oci-containers.backend = lib.mkDefault "docker";
|
||||
virtualisation.oci-containers.containers.csbie = {
|
||||
image = cfg.image;
|
||||
ports = [
|
||||
"${toString cfg.port}:8787"
|
||||
];
|
||||
environmentFiles = [
|
||||
cfg.envFile
|
||||
];
|
||||
volumes = [
|
||||
"${cfg.dataDir}:/app/data"
|
||||
];
|
||||
};
|
||||
})
|
||||
|
||||
virtualisation.oci-containers.containers.csbie = {
|
||||
image = cfg.image;
|
||||
ports = [
|
||||
"${toString cfg.port}:8787"
|
||||
];
|
||||
environmentFiles = [
|
||||
cfg.envFile
|
||||
];
|
||||
volumes = [
|
||||
"${cfg.dataDir}:/app/data"
|
||||
];
|
||||
};
|
||||
};
|
||||
(lib.mkIf (cfg.runtime == "native") {
|
||||
systemd.services.csbie = {
|
||||
description = "cSBI";
|
||||
wantedBy = [
|
||||
"multi-user.target"
|
||||
];
|
||||
after = [
|
||||
"network-online.target"
|
||||
];
|
||||
wants = [
|
||||
"network-online.target"
|
||||
];
|
||||
environment = {
|
||||
CSBIE_ENV_FILE = toString cfg.envFile;
|
||||
CSBIE_DATA_DIR = cfg.dataDir;
|
||||
PORT = toString cfg.port;
|
||||
};
|
||||
serviceConfig = {
|
||||
Type = "simple";
|
||||
Restart = "on-failure";
|
||||
RestartSec = "5s";
|
||||
};
|
||||
script = ''
|
||||
exec ${nativeRun}/bin/csbi-native-run
|
||||
'';
|
||||
};
|
||||
})
|
||||
]);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user