u/flake.nix

130 lines
3.7 KiB
Nix
Raw Normal View History

2023-04-28 14:31:51 -04:00
{
description = "blazingly-fast super-concurrent webscale file server";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
crane.url = "github:ipetkov/crane";
crane.inputs.nixpkgs.follows = "nixpkgs";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, crane, flake-utils }:
let
description = "blazingly-fast super-concurrent webscale file server";
src = ./.;
packages = flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages."${system}";
craneLib = crane.lib.${system};
in rec {
packages.u = craneLib.buildPackage {
pname = "u";
version = "0.1.0";
# Don't use cleanCargoSource here, as it removes sqlx-data.json
src = src;
doCheck = false;
};
apps.u = flake-utils.lib.mkApp {
name = "u";
drv = packages.u;
};
packages.u-upload = pkgs.stdenv.mkDerivation rec {
name = "u-upload";
src =
(pkgs.writeScriptBin "u-upload" (builtins.readFile ./upload.sh));
buildInputs = with pkgs; [ curl jq xclip ];
nativeBuildInputs = with pkgs; [ makeWrapper ];
installPhase = ''
mkdir -p $out/bin
cp bin/u-upload $out/bin
wrapProgram $out/bin/u-upload \
--prefix PATH : ${pkgs.lib.makeBinPath buildInputs}
'';
};
devShell = pkgs.mkShell {
inputsFrom = [ packages.u ];
nativeBuildInputs = with pkgs; [
rustc
cargo
rust-analyzer
sqlx-cli
packages.u-upload
];
};
});
in packages // {
nixosModule = { config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.u;
pkg = self.packages.${pkgs.system}.u;
in {
options.services.u = {
enable = mkEnableOption description;
user = mkOption {
type = types.str;
default = "u";
};
group = mkOption {
type = types.str;
default = "u";
};
port = mkOption {
type = types.int;
default = 8075;
};
};
config = mkIf cfg.enable {
systemd.services.u = {
description = description;
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
preStart = ''
cat > /var/lib/u/.env <<EOF
DATABASE_URL="sqlite:/var/lib/u/u.db"
U_PORT="${toString cfg.port}"
U_FILES_DIR="/var/lib/u/files"
EOF
# Create and migrate the database
${pkgs.sqlx-cli}/bin/sqlx database create
${pkgs.sqlx-cli}/bin/sqlx migrate run --source "${src}/migrations"
'';
script = "${pkg}/bin/u";
serviceConfig = {
User = cfg.user;
Group = cfg.group;
Restart = "always";
WorkingDirectory = "/var/lib/u";
};
};
users = {
users = mkIf (cfg.user == "u") {
u = {
home = "/var/lib/u";
createHome = true;
group = cfg.group;
isSystemUser = true;
};
};
groups = mkIf (cfg.group == "u") { u = { }; };
};
};
};
};
}