flake rework, sqlx 0.7

This commit is contained in:
Robin Appelman 2024-02-11 14:09:52 +01:00
commit 0842c3e136
10 changed files with 1021 additions and 409 deletions

View file

@ -0,0 +1,15 @@
{
"db_name": "PostgreSQL",
"query": "INSERT INTO logs_raw(id, json) VALUES($1, $2)",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int4",
"Jsonb"
]
},
"nullable": []
},
"hash": "bcd0843c15afc0443df1ab40d7d0c5632ab9d025cdcf3646f4e9d5872b7c8c89"
}

View file

@ -0,0 +1,20 @@
{
"db_name": "PostgreSQL",
"query": "SELECT MAX(id) AS last_archived FROM logs_raw",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "last_archived",
"type_info": "Int4"
}
],
"parameters": {
"Left": []
},
"nullable": [
null
]
},
"hash": "e4dfb1fbca2c91ac837d11aff64e035ccce38867e3503653ec2ef28eeeb8907d"
}

1062
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,10 +5,10 @@ authors = ["Robin Appelman <robin@icewind.nl>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
sqlx = { version = "0.6.0", default_features = false, features = ["macros", "postgres", "json", "runtime-actix-rustls", "offline"] } sqlx = { version = "0.7.3", default_features = false, features = ["macros", "postgres", "json", "runtime-tokio-rustls"] }
dotenv = "0.15.0" dotenv = "0.15.0"
main_error = "0.1.2" main_error = "0.1.2"
tokio = { version = "1.20.0", features = ["macros", "time"] } tokio = { version = "1.20.0", features = ["macros", "time", "rt-multi-thread"] }
reqwest = { version = "0.11.11", default-features = false, features = ["rustls-tls"] } reqwest = { version = "0.11.11", default-features = false, features = ["rustls-tls"] }
serde = { version = "1.0.140", features = ["derive"] } serde = { version = "1.0.140", features = ["derive"] }
serde_json = "1.0.82" serde_json = "1.0.82"

72
flake.lock generated
View file

@ -1,67 +1,57 @@
{ {
"nodes": { "nodes": {
"flake-utils": { "flake-utils": {
"locked": {
"lastModified": 1656928814,
"narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"naersk": {
"inputs": { "inputs": {
"nixpkgs": "nixpkgs" "systems": "systems"
}, },
"locked": { "locked": {
"lastModified": 1655042882, "lastModified": 1705309234,
"narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=", "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "nix-community", "owner": "numtide",
"repo": "naersk", "repo": "flake-utils",
"rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f", "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "nix-community", "owner": "numtide",
"repo": "naersk", "repo": "flake-utils",
"type": "github" "type": "github"
} }
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 0, "lastModified": 1707650010,
"narHash": "sha256-WbZKCf6g4+dljZ6Z866SGZzNA+WWjXt4Lc7td4zsRkw=", "narHash": "sha256-dOhphIA4MGrH4ElNCy/OlwmN24MsnEqFjRR6+RY7jZw=",
"path": "/nix/store/vavqvhqw8x42kqb1m3wkcys65jvvnl5i-source", "owner": "NixOS",
"type": "path" "repo": "nixpkgs",
}, "rev": "809cca784b9f72a5ad4b991e0e7bcf8890f9c3a6",
"original": { "type": "github"
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 0,
"narHash": "sha256-WbZKCf6g4+dljZ6Z866SGZzNA+WWjXt4Lc7td4zsRkw=",
"path": "/nix/store/vavqvhqw8x42kqb1m3wkcys65jvvnl5i-source",
"type": "path"
}, },
"original": { "original": {
"id": "nixpkgs", "id": "nixpkgs",
"ref": "release-23.11",
"type": "indirect" "type": "indirect"
} }
}, },
"root": { "root": {
"inputs": { "inputs": {
"flake-utils": "flake-utils", "flake-utils": "flake-utils",
"naersk": "naersk", "nixpkgs": "nixpkgs"
"nixpkgs": "nixpkgs_2" }
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
} }
} }
}, },

114
flake.nix
View file

@ -1,110 +1,54 @@
{ {
inputs = { inputs = {
nixpkgs.url = "nixpkgs/release-23.11";
flake-utils.url = "github:numtide/flake-utils"; flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
}; };
outputs = { outputs = {
self, self,
nixpkgs, nixpkgs,
flake-utils, flake-utils,
naersk,
}: }:
flake-utils.lib.eachDefaultSystem ( flake-utils.lib.eachDefaultSystem (
system: let system: let
pkgs = nixpkgs.legacyPackages."${system}"; overlays = [
naersk-lib = naersk.lib."${system}"; (import ./overlay.nix)
];
pkgs = import nixpkgs {
inherit system overlays;
};
in rec { in rec {
# `nix build` packages = rec {
packages.log-archiver = naersk-lib.buildPackage { inherit (pkgs) log-archiver;
pname = "log-archiver"; default = log-archiver;
root = ./.;
}; };
defaultPackage = packages.log-archiver;
defaultApp = packages.log-archiver;
# `nix develop` devShells.default = pkgs.mkShell {
devShell = pkgs.mkShell { nativeBuildInputs = with pkgs; [
nativeBuildInputs = with pkgs; [rustc cargo bacon cargo-edit cargo-outdated]; rustc
cargo
bacon
clippy
cargo-edit
cargo-outdated
cargo-insta
sqlx-cli
];
}; };
} }
) )
// { // {
nixosModule = { overlays.default = import ./overlay.nix;
nixosModules.default = {
pkgs,
config, config,
lib, lib,
pkgs,
... ...
}: }: {
with lib; let imports = [./module.nix];
cfg = config.services.log-archiver; config = lib.mkIf config.services.log-archiver.enable {
in { nixpkgs.overlays = [self.overlays.default];
options.services.log-archiver = { services.log-archiver.package = lib.mkDefault pkgs.log-archiver;
enable = mkEnableOption "Log archiver";
user = mkOption {
type = types.str;
description = "user to save logs as";
};
databaseUrlFile = mkOption {
type = types.str;
description = "file containg DATABASE_URL variable";
};
baseUrl = mkOption {
type = types.str;
default = "https://logs.tf";
description = "logs.tf base url";
};
target = mkOption {
type = types.str;
description = "path to store logs to";
};
};
config = mkIf cfg.enable {
systemd.services."log-archiver" = let
pkg = self.defaultPackage.${pkgs.system};
in {
wantedBy = ["multi-user.target"];
script = "${pkg}/bin/log-archiver";
environment = {
API_BASE = cfg.baseUrl;
LOG_TARGET = cfg.target;
};
serviceConfig = {
EnvironmentFile = cfg.databaseUrlFile;
Restart = "on-failure";
User = cfg.user;
PrivateTmp = true;
ProtectSystem = "strict";
ReadWritePaths = cfg.target;
ProtectHome = true;
NoNewPrivileges = true;
PrivateDevices = true;
ProtectClock = true;
CapabilityBoundingSet = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
SystemCallArchitectures = "native";
ProtectKernelModules = true;
RestrictNamespaces = true;
MemoryDenyWriteExecute = true;
ProtectHostname = true;
LockPersonality = true;
ProtectKernelTunables = true;
RestrictAddressFamilies = "AF_INET AF_INET6 AF_UNIX";
RestrictRealtime = true;
ProtectProc = "noaccess";
SystemCallFilter = ["@system-service" "~@resources" "~@privileged"];
IPAddressDeny = "multicast";
PrivateUsers = true;
ProcSubset = "pid";
};
};
}; };
}; };
}; };

80
module.nix Normal file
View file

@ -0,0 +1,80 @@
{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.services.log-archiver;
in {
options.services.log-archiver = {
enable = mkEnableOption "Log archiver";
user = mkOption {
type = types.str;
description = "user to save logs as";
};
databaseUrlFile = mkOption {
type = types.str;
description = "file containg DATABASE_URL variable";
};
baseUrl = mkOption {
type = types.str;
default = "https://logs.tf";
description = "logs.tf base url";
};
target = mkOption {
type = types.str;
description = "path to store logs to";
};
package = mkOption {
type = types.package;
description = "package to use";
};
};
config = mkIf cfg.enable {
systemd.services."log-archiver" = {
wantedBy = ["multi-user.target"];
environment = {
API_BASE = cfg.baseUrl;
LOG_TARGET = cfg.target;
};
serviceConfig = {
EnvironmentFile = cfg.databaseUrlFile;
ExecStart = "${cfg.package}/bin/log-archiver";
Restart = "on-failure";
User = cfg.user;
PrivateTmp = true;
ProtectSystem = "strict";
ReadWritePaths = cfg.target;
ProtectHome = true;
NoNewPrivileges = true;
PrivateDevices = true;
ProtectClock = true;
CapabilityBoundingSet = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
SystemCallArchitectures = "native";
ProtectKernelModules = true;
RestrictNamespaces = true;
MemoryDenyWriteExecute = true;
ProtectHostname = true;
LockPersonality = true;
ProtectKernelTunables = true;
RestrictAddressFamilies = "AF_INET AF_INET6 AF_UNIX";
RestrictRealtime = true;
ProtectProc = "noaccess";
SystemCallFilter = ["@system-service" "~@resources" "~@privileged"];
IPAddressDeny = "multicast";
PrivateUsers = true;
ProcSubset = "pid";
};
};
};
}

3
overlay.nix Normal file
View file

@ -0,0 +1,3 @@
final: prev: {
log-archiver = final.callPackage ./package.nix {};
}

20
package.nix Normal file
View file

@ -0,0 +1,20 @@
{
stdenv,
rustPlatform,
lib,
}: let
inherit (lib.sources) sourceByRegex;
src = sourceByRegex ./. ["Cargo.*" "(src|tests|.sqlx)(/.*)?"];
in
rustPlatform.buildRustPackage rec {
pname = "log-archiver";
version = "0.1.0";
SQLX_OFFLINE = 1;
inherit src;
cargoLock = {
lockFile = ./Cargo.lock;
};
}

View file

@ -1,34 +0,0 @@
{
"db": "PostgreSQL",
"bcd0843c15afc0443df1ab40d7d0c5632ab9d025cdcf3646f4e9d5872b7c8c89": {
"query": "INSERT INTO logs_raw(id, json) VALUES($1, $2)",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Int4",
"Jsonb"
]
},
"nullable": []
}
},
"e4dfb1fbca2c91ac837d11aff64e035ccce38867e3503653ec2ef28eeeb8907d": {
"query": "SELECT MAX(id) AS last_archived FROM logs_raw",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "last_archived",
"type_info": "Int4"
}
],
"parameters": {
"Left": []
},
"nullable": [
null
]
}
}
}