Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
description = "Hackfed daemon";

inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};

outputs = inputs@{ flake-parts, ... }:
let
mkPackage = pkgs: pkgs.callPackage ./nix/package.nix {};
in flake-parts.lib.mkFlake { inherit inputs; } {
systems = [ "x86_64-linux" "aarch64-linux" "aarch64-darwin" "x86_64-darwin" ];
perSystem = { config, self', inputs', pkgs, system, ... }: {
devShells.default = pkgs.mkShell {
packages = with pkgs; [
bun
];
};
packages = let
hackfedd = mkPackage pkgs;
in {
inherit hackfedd;
default = hackfedd;
};
};
flake = {
overlays.default = final: prev: {
hackfedd = mkPackage prev;
};
nixosModules.default = { imports = [ ./nix/module.nix ]; };
};
};
}
81 changes: 81 additions & 0 deletions nix/module.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{ config, lib, pkgs, ... }: let
hackfedd = pkgs.callPackage ./package.nix {};
cfg = config.services.hackfedd;
defaultUser = "hackfedd";
defaultGroup = "hackfedd";
format = pkgs.formats.yaml { };
configFile = format.generate "config.yml" cfg.settings;
in with lib; {
options.services.hackfedd = {
enable = mkEnableOption "hackfedd service";

settings = mkOption {
default = { };
description = ''
Configuration for hackfedd. See
<https://github.com/hackfed/hackfedd/blob/drafts/base/examples/config.example.yaml>
for more information.
'';
type = types.submodule {
freeformType = format.type;
};
example = literalExpression ''
{
general = {
org_id = "acme";
};
}
'';
};

package = mkOption {
type = types.package;
default = hackfedd;
defaultText = literalExpression "pkgs.callPackage ./package.nix {}";
};

user = mkOption {
description = "User account under which hackfedd service runs.";
default = defaultUser;
type = types.str;
};

group = mkOption {
description = "Group under which hackfedd service runs.";
default = defaultGroup;
type = types.str;
};
};

config = mkIf cfg.enable {
environment.systemPackages = [ cfg.package ];

users.users = mkIf (cfg.user == defaultUser) {
hackfedd = {
description = "hackfedd service user";
isSystemUser = true;
group = cfg.group;
home = "/var/lib/hackfedd";
createHome = true;
};
};

users.groups = mkIf (cfg.group == defaultGroup) {
hackfedd = { };
};

systemd.services.hackfedd = {
description = "hackfedd service";
after = [ "network.target" ];
wantedBy = [ "multi-user.target" ];
serviceConfig = {
Type = "simple";
User = cfg.user;
Group = cfg.group;
WorkingDirectory = "/var/lib/hackfedd";
ExecStart = "${getExe cfg.package} agent --config ${configFile}";
Restart = "on-failure";
};
};
};
}
55 changes: 55 additions & 0 deletions nix/node-modules.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# References:
# https://github.com/NixOS/nixpkgs/blob/c79db2f14a7deda260c8cd4f5e66f1b059321cee/pkgs/by-name/eq/equibop/package.nix
# https://github.com/NixOS/nixpkgs/blob/c79db2f14a7deda260c8cd4f5e66f1b059321cee/pkgs/by-name/op/opencode/package.nix
{
lib,
stdenvNoCC,
hackfedd,
bun,
writableTmpDirAsHomeHook,
}:
stdenvNoCC.mkDerivation {
inherit (hackfedd) version src;
pname = hackfedd.pname + "-modules";

impureEnvVars = lib.fetchers.proxyImpureEnvVars ++ [
"GIT_PROXY_COMMAND"
"SOCKS_SERVER"
];

nativeBuildInputs = [
bun
writableTmpDirAsHomeHook
];

dontConfigure = true;
dontFixup = true;

buildPhase = ''
runHook preBuild

export BUN_INSTALL_CACHE_DIR=$(mktemp -d)

bun install \
--cpu "*" \
--os "*" \
--filter ./ \
--frozen-lockfile \
--ignore-scripts \
--no-progress

runHook postBuild
'';

installPhase = ''
runHook preInstall

cp -R ./node_modules $out

runHook postInstall
'';

outputHash = "sha256-uLJo1yvsd3EMqidgKt47WHCXl+IZ2Ix8JZd2YtPh6ME=";
outputHashAlgo = "sha256";
outputHashMode = "recursive";
}
76 changes: 76 additions & 0 deletions nix/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
lib,
stdenv,
callPackage,
makeWrapper,
bun,
nodejs,
ast-grep,
writableTmpDirAsHomeHook,
}:
stdenv.mkDerivation (finalAttrs: {
pname = "hackfedd";
version = "0.0.0";

src = ../.;

node-modules = callPackage ./node-modules.nix { hackfedd = finalAttrs.finalPackage; }; # helps when the parent package is overidden

nativeBuildInputs = [
bun
nodejs
makeWrapper
writableTmpDirAsHomeHook
ast-grep
];

configurePhase = ''
runHook preConfigure

cp -R ${finalAttrs.node-modules} node_modules
chmod u+w -R node_modules

# https://github.com/oven-sh/bun/issues/13076
substituteInPlace node_modules/css-tree/package.json \
--replace-fail './lib/index.js' './cjs/index.cjs'

# jsdom uses fs.readFile to load assets from relative paths, replacing them to bundled paths
ast-grep -U -p 'path.resolve(__dirname, "../../../browser/default-stylesheet.css")' --rewrite 'path.resolve(import.meta.dir, "default-stylesheet.css")' -l js node_modules/jsdom/lib/jsdom/living/css/helpers/computed-style.js
ast-grep -U -p 'require.resolve("./xhr-sync-worker.js")' --rewrite 'require("node:path").resolve(import.meta.dir, "xhr-sync-worker.js")' -l js node_modules/jsdom/lib/jsdom/living/xhr/XMLHttpRequest-impl.js

runHook postConfigure
'';

buildPhase = ''
runHook preBuild
bun build --compile --outfile=hackfedd \
--asset ./node_modules/jsdom/lib/jsdom/browser/default-stylesheet.css \
--asset ./node_modules/jsdom/lib/jsdom/living/xhr/xhr-sync-worker.js \
./src/index.ts
runHook postBuild
'';

installPhase = ''
runHook preInstall

mkdir -p $out/bin
cp hackfedd $out/bin

runHook postInstall
'';

dontStrip = true;

meta = {
description = "Daemon for Hackerspace Federation";
homepage = "https://github.com/hackfed/hackfedd";
license = lib.licenses.mpl20;
maintainers = [ {
name = "Damir Modyarov";
email = "damir@otomir23.me";
github = "otomir23";
githubId = 21289906;
} ];
mainProgram = "hackfedd";
};
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
},
"repository": {
"type": "git",
"url": "git+https://github.com/hackfed/hfd.git"
"url": "git+https://github.com/hackfed/hackfedd.git"
},
"scripts": {
"lint": "tsc --noEmit && eslint .",
Expand Down