From 5a151725138c931fbc9b12c0f4991366f01d3656 Mon Sep 17 00:00:00 2001 From: Shotaro Nakamura Date: Fri, 19 Jun 2026 14:53:51 +0900 Subject: [PATCH] add nix way --- README.md | 42 +++++++++++++++ compose.yml | 2 +- flake.nix | 152 +++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 175 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 8612f16..24ced37 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,45 @@ [![技術者倫理 遵守済み](https://img.shields.io/badge/%E6%8A%80%E8%A1%93%E8%80%85%E5%80%AB%E7%90%86-%E9%81%B5%E5%AE%88%E6%B8%88%E3%81%BF-0a0a0a?style=for-the-badge&labelColor=ffffff)](https://技術者倫理.com) A client for SBI-compatible servers. This project is not intended for use with a real SBI server. + +## cSBI SDK + +## cSBIe + +cSBIe is a web ui and server for managing SBI-compatible API. + +### Install + +Before running, setup .env, refering `.env.example`. + +#### With docker + +docker-compose.yml: + +```yaml +services: + csbie: + image: git.yutakobayashi.com/nakasyou/csbi:latest + ports: + - '18787:8787' + env_file: + - .env + volumes: + - ./data:/app/data +``` + +or + +```bash +docker run --rm -it --name csbie \ + -p 18787:8787 \ + --env-file .env \ + -v "$PWD/data:/app/data" \ + git.yutakobayashi.com/nakasyou/csbi:latest +``` + +#### With Nix + +```bash +nix run 'git+https://git.yutakobayashi.com/nakasyou/csbi.git#csbie' +``` diff --git a/compose.yml b/compose.yml index 912dffb..83a8909 100644 --- a/compose.yml +++ b/compose.yml @@ -2,7 +2,7 @@ services: csbie: image: git.yutakobayashi.com/nakasyou/csbi:latest ports: - - '18787:8787' + - '${CSBIE_HOST_PORT:-18787}:8787' env_file: - .env volumes: diff --git a/flake.nix b/flake.nix index 3b1f1f3..6d1b647 100644 --- a/flake.nix +++ b/flake.nix @@ -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 + ''; + }; + }) + ]); }; }; }