Fix nix builds: go dependencies are awful

This commit is contained in:
bluepython508
2023-11-29 15:41:06 +00:00
parent 0926de2bd8
commit 259f002a99
11 changed files with 355 additions and 124 deletions

108
flake.nix
View File

@@ -3,47 +3,55 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
gomod2nix.url = "github:nix-community/gomod2nix";
gomod2nix.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs, gomod2nix, systems }: let
eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f rec {
inherit system;
pkgs = nixpkgs.legacyPackages.${system};
goModPkgs = gomod2nix.legacyPackages.${system};
inherit (pkgs) callPackage;
});
outputs = {
self,
nixpkgs,
systems,
}: let
eachSystem = f:
nixpkgs.lib.genAttrs (import systems) (system:
f rec {
inherit system;
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) callPackage;
});
in {
packages = eachSystem ({ goModPkgs, callPackage, ...}: {
default = callPackage ./. {
inherit (goModPkgs) buildGoApplication;
};
packages = eachSystem ({callPackage, ...}: {
default = callPackage ./. {};
});
devShells = eachSystem ({goModPkgs, callPackage, ...}: {
default = callPackage ./shell.nix {
inherit (goModPkgs) mkGoEnv gomod2nix;
};
devShells = eachSystem ({callPackage, ...}: {
default = callPackage ./shell.nix {};
});
nixosModules.default = { config, pkgs, lib, ... }: {
options.services.bluepython508.tsnet-proxy = with lib; with types; {
nixosModules.default = {
config,
pkgs,
lib,
...
}: {
options.services.bluepython508.tsnet-proxy = with lib;
with types; {
clientId = mkOption {
type = str;
};
clientSecretFile = mkOption {
type = str;
};
tags = mkOption { type = listOf str; };
tags = mkOption {type = listOf str;};
proxies = mkOption {
type = attrsOf (submodule ({ config }: {
type = attrsOf (submodule ({config, ...}: {
options = let
proto = enum ["udp" "tcp" "unix"];
in {
enable = mkOption { type = bool; default = true; };
proto = mkOption { type = proto; };
dest = mkOption { type = str; };
hostProto = mkOption { type = proto; };
port = mkOption { type = int; };
enable = mkOption {
type = bool;
default = true;
};
proto = mkOption {type = proto;};
dest = mkOption {type = str;};
hostProto = mkOption {type = proto;};
port = mkOption {type = port;};
};
config.hostProto = mkDefault config.proto;
}));
@@ -51,23 +59,39 @@
};
config.systemd.services = let
cfg = config.services.bluepython508.tsnet-proxy;
get-authkey = pkgs.tailscale.overrideAttrs { subPackages = ["cmd/get-authkey"]; postInstall = ""; };
in lib.mapAttrs (hostname: { proto, hostProto, port, dest, enable, ... }: {
inherit enable;
script = ''
TS_AUTHKEY=$(cat $RUNTIME_DIRECTORY/authkey) ${lib.getExe self.packages.${pkgs.system}.default} ${hostProto} ${hostname} ${port} ${proto} ${dest}
'';
wantedBy = [ "multi-user.target" ];
serviceConfig = {
DynamicUser = true;
RuntimeDirectory = hostname;
ExecStartPre = "!${pkgs.writeShellScript "get-authkey" ''
TS_API_CLIENT_ID=${cfg.clientId} TS_API_CLIENT_SECRET=$(cat ${cfg.clientSecretFile}) ${get-authkey}/bin/get-authkey -ephemeral -tags ${lib.concatStringsSep "," cfg.tags} > $RUNTIME_DIRECTORY/authkey
chown ${hostname}:${hostname} $RUNTIME_DIRECTORY/authkey
''}";
get-authkey = pkgs.tailscale.overrideAttrs {
subPackages = ["cmd/get-authkey"];
postInstall = "";
};
}) cfg.proxies;
in
lib.mapAttrs' (hostname: {
proto,
hostProto,
port,
dest,
enable,
...
}: let
name = "tsnet-proxy-${hostname}";
in {
inherit name;
value = {
inherit enable;
script = ''
TS_AUTHKEY=$(cat $RUNTIME_DIRECTORY/authkey) ${lib.getExe self.packages.${pkgs.system}.default} ${hostProto} ${hostname} ${toString port} ${proto} ${dest}
'';
wantedBy = ["multi-user.target"];
serviceConfig = {
DynamicUser = true;
RuntimeDirectory = name;
ExecStartPre = "!${pkgs.writeShellScript "get-authkey" ''
TS_API_CLIENT_ID=${cfg.clientId} TS_API_CLIENT_SECRET=$(cat ${cfg.clientSecretFile}) ${get-authkey}/bin/get-authkey -ephemeral -tags ${lib.concatStringsSep "," cfg.tags} > $RUNTIME_DIRECTORY/authkey
chown ${name}:${name} $RUNTIME_DIRECTORY/authkey
''}";
};
};
})
cfg.proxies;
};
};
}