actual-nix/flake.nix

172 lines
4.4 KiB
Nix
Raw Normal View History

2024-05-09 07:33:20 -04:00
{
description = "Actual budget app";
2024-05-09 18:56:54 -04:00
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
2024-05-09 07:33:20 -04:00
outputs = { self, nixpkgs }: let
pkgs = import nixpkgs {
system = "x86_64-linux";
};
pname = "actual-server";
2024-12-07 00:57:07 -05:00
version = "24.12.0";
2024-05-09 07:33:20 -04:00
src = pkgs.fetchFromGitHub {
owner = "actualbudget";
repo = pname;
rev = "v${version}";
2024-12-07 00:57:07 -05:00
sha256 = "sha256-qCATfpYjDlR2LaalkF0/b5tD4HDE4aNDrLvTC4g0ctY=";
2024-05-09 07:33:20 -04:00
};
package = pkgs.stdenv.mkDerivation (finalAttrs: {
inherit pname version src;
nativeBuildInputs = with pkgs; [
yarn-berry
nodejs
python3
jq
moreutils
makeWrapper
];
yarnOfflineCache = pkgs.stdenvNoCC.mkDerivation {
name = "actual-deps";
nativeBuildInputs = with pkgs; [ yarn-berry ];
inherit (finalAttrs) src;
NODE_EXTRA_CA_CERTS = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
supportedArchitectures = builtins.toJSON {
os = [ "darwin" "linux" ];
cpu = [ "arm" "arm64" "ia32" "x64" ];
libc = [ "glibc" "musl" ];
};
configurePhase = ''
runHook preConfigure
export HOME="$NIX_BUILD_TOP"
export YARN_ENABLE_TELEMETRY=0
yarn config set enableGlobalCache false
yarn config set cacheFolder $out
yarn config set supportedArchitectures --json "$supportedArchitectures"
runHook postConfigure
'';
buildPhase = ''
runHook preBuild
mkdir -p $out
yarn install --immutable --mode skip-build
runHook postBuild
'';
dontInstall = true;
outputHashAlgo = "sha256";
2024-12-07 00:57:07 -05:00
outputHash = "sha256-hk2uUdg77lTm8e8WmLjYWzNinre0ag/BHc+knl/Q1xo=";
2024-05-09 07:33:20 -04:00
outputHashMode = "recursive";
};
patchPhase = ''
sed -i '1i#!${pkgs.nodejs}/bin/node' app.js
'';
configurePhase = ''
runHook preConfigure
export HOME="$NIX_BUILD_TOP"
export YARN_ENABLE_TELEMETRY=0
export npm_config_nodedir=${pkgs.nodejs}
yarn config set enableGlobalCache false
yarn config set cacheFolder $yarnOfflineCache
runHook postConfigure
'';
buildPhase = ''
runHook preBuild
2024-05-09 07:57:53 -04:00
yarn install --immutable --immutable-cache
2024-05-09 07:33:20 -04:00
yarn build
yarn workspaces focus --all --production
runHook postBuild
'';
installPhase = ''
runHook preInstall
mkdir -p $out/{bin,lib}
mkdir $out/lib/actual
cp -r package.json app.js src migrations node_modules $out/lib/actual/
chmod +x $out/lib/actual/app.js
2024-05-09 07:58:06 -04:00
makeWrapper $out/lib/actual/app.js $out/bin/actual --chdir $out/lib/actual
2024-05-09 07:33:20 -04:00
runHook postInstall
'';
fixupPhase = ''
runHook preFixup
patchShebangs $out/lib
runHook postFixup
'';
});
in rec {
packages.x86_64-linux = {
"${pname}" = package;
default = package;
};
2024-05-09 19:14:35 -04:00
nixosModules.default = { lib, config, pkgs, ... }:
2024-05-09 18:46:45 -04:00
with lib;
let
2024-05-09 19:12:56 -04:00
cfg = config.services.actual;
2024-05-09 18:46:45 -04:00
dataDir = "/var/lib/actual";
2024-05-09 19:14:35 -04:00
cfgFile = pkgs.writeText "actual.json" (builtins.toJSON {
2024-05-09 18:46:45 -04:00
inherit dataDir;
inherit (cfg) hostname port;
serverFiles = "${dataDir}/server-files";
userFiles = "${dataDir}/user-files";
});
in {
options.services.actual = {
enable = mkEnableOption "Actual budget server";
hostname = mkOption { type = types.str; default = "127.0.0.1"; };
port = mkOption { type = types.port; default = 5006; };
};
config = mkIf cfg.enable {
users.users.actual = {
name = "actual";
group = "actual";
isSystemUser = true;
};
users.groups.actual = {};
systemd.services.actual-server = {
description = "Actual budget server";
documentation = [ "https://actualbudget.org/docs/" ];
wantedBy = [ "multi-user.target" ];
after = [ "networking.target" ];
serviceConfig = {
ExecStart = "${package}/bin/actual";
Restart = "always";
User = "actual";
Group = "actual";
PrivateTmp = true;
StateDirectory = "actual";
};
environment.ACTUAL_CONFIG_PATH = "${cfgFile}";
};
};
};
2024-05-09 07:33:20 -04:00
};
}