flake reorg

This commit is contained in:
Robin Appelman 2024-01-14 19:26:53 +01:00
commit f5fd05dec1
9 changed files with 691 additions and 467 deletions

16
.github/workflows/ci.yaml vendored Normal file
View file

@ -0,0 +1,16 @@
on: [push, pull_request]
name: Continuous integration
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v20
- uses: icewind1991/attic-action@v1
with:
name: ci
instance: https://cache.icewind.me
authToken: '${{ secrets.ATTIC_TOKEN }}'
- run: nix build

774
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,16 +1,16 @@
[package]
name = "mover"
name = "demostf-mover"
version = "0.1.0"
edition = "2021"
[dependencies]
thiserror = "1.0.30"
demostf-client = { version = "0.4.1", default-features = false, features = ["rustls-tls"] }
tokio = { version = "1.17.0", features = ["rt-multi-thread", "macros"] }
tracing = "0.1.33"
tracing-subscriber = "0.3.11"
futures-util = "0.3.21"
thiserror = "1.0.56"
demostf-client = { version = "0.4.3", default-features = false, features = ["rustls-tls"] }
tokio = { version = "1.35.1", features = ["rt-multi-thread", "macros"] }
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
futures-util = "0.3.30"
main_error = "0.1.2"
dotenv = "0.15.0"
time = "0.3.9"
md5 = "0.7.0"
dotenvy = "0.15.7"
time = "0.3.31"
md5 = "0.7.0"

72
flake.lock generated
View file

@ -1,67 +1,57 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1656065134,
"narHash": "sha256-oc6E6ByIw3oJaIyc67maaFcnjYOz1mMcOtHxbEf9NwQ=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "bee6a7250dd1b01844a2de7e02e4df7d8a0a206c",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"naersk": {
"inputs": {
"nixpkgs": "nixpkgs"
"systems": "systems"
},
"locked": {
"lastModified": 1655042882,
"narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
"owner": "nix-community",
"repo": "naersk",
"rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
"lastModified": 1701680307,
"narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "naersk",
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 0,
"narHash": "sha256-A9RkoGrxzsmMm0vily18p92Rasb+MbdDMaSnzmywXKw=",
"path": "/nix/store/dycwkjqxlc93s7ch3c932d0f9xvfch33-source",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 0,
"narHash": "sha256-A9RkoGrxzsmMm0vily18p92Rasb+MbdDMaSnzmywXKw=",
"path": "/nix/store/dycwkjqxlc93s7ch3c932d0f9xvfch33-source",
"type": "path"
"lastModified": 1705183652,
"narHash": "sha256-rnfkyUH0x72oHfiSDhuCHDHg3gFgF+lF8zkkg5Zihsw=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "428544ae95eec077c7f823b422afae5f174dee4b",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "nixos-23.11",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"naersk": "naersk",
"nixpkgs": "nixpkgs_2"
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},

157
flake.nix
View file

@ -1,143 +1,46 @@
{
inputs = {
nixpkgs.url = "nixpkgs/nixos-23.11";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
};
outputs = { self, nixpkgs, flake-utils, naersk }:
outputs = {
self,
nixpkgs,
flake-utils,
}:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = nixpkgs.legacyPackages."${system}";
naersk-lib = naersk.lib."${system}";
in
rec {
# `nix build`
packages.demomover = naersk-lib.buildPackage {
pname = "demomover";
root = ./.;
};
defaultPackage = packages.demomover;
overlays = [
(import ./overlay.nix)
];
pkgs = (import nixpkgs) {
inherit system overlays;
};
in rec {
packages = rec {
demostf-mover = pkgs.demostf-mover;
default = demostf-mover;
};
# `nix run`
apps.hello-world = flake-utils.lib.mkApp {
drv = packages.demomover;
};
defaultApp = apps.demomover;
# `nix develop`
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ rustc cargo bacon ];
};
}
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [rustc cargo bacon clippy cargo-audit cargo-edit];
};
}
)
// {
nixosModule = {
overlays.default = import ./overlay.nix;
nixosModules.default = {
pkgs,
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.services.demosmover;
in {
options.services.demosmover = {
enable = mkEnableOption "Enables the demos mover service";
source = mkOption {
type = types.str;
description = "source directory";
};
target = mkOption {
type = types.str;
description = "target directory";
};
sourceBackend = mkOption {
type = types.str;
description = "source backend";
};
targetBackend = mkOption {
type = types.str;
description = "target backend";
};
keyFile = mkOption {
type = types.str;
description = "file containing the api key";
};
age = mkOption {
type = types.int;
default = 78894000;
description = "age of files to move in secconds";
};
logLevel = mkOption {
type = types.str;
default = "INFO";
description = "log level";
};
user = mkOption {
type = types.str;
description = "user that owns the demos";
};
interval = mkOption {
type = types.str;
default = "*:0/10";
description = "Interval to run the service";
};
};
config = mkIf cfg.enable {
systemd.services.demosmover = let
pkg = self.defaultPackage.${pkgs.system};
in {
script = "${pkg}/bin/mover";
description = "Move demos for demos.tf";
environment = {
SOURCE_ROOT = cfg.source;
TARGET_ROOT = cfg.target;
SOURCE_BACKEND = cfg.sourceBackend;
TARGET_BACKEND = cfg.targetBackend;
AGE = toString cfg.age;
RUST_LOG = cfg.logLevel;
};
serviceConfig = {
EnvironmentFile = cfg.keyFile;
ReadWritePaths = [cfg.source cfg.target];
Restart = "on-failure";
User = cfg.user;
PrivateTmp = true;
ProtectSystem = "strict";
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";
RestrictRealtime = true;
ProtectProc = "noaccess";
SystemCallFilter = ["@system-service" "~@resources" "~@privileged"];
IPAddressDeny = "localhost link-local multicast";
};
};
systemd.timers.demosmover = {
enable = true;
description = "Move demos for demos.tf";
wantedBy = ["multi-user.target"];
timerConfig = {
OnCalendar = "*:0/10";
};
};
};
}: {
imports = [./module.nix];
config = lib.mkIf config.services.demostf-mover.enable {
nixpkgs.overlays = [self.overlays.default];
services.demostf-mover.package = lib.mkDefault pkgs.demostf-mover;
};
};
};
}

112
module.nix Normal file
View file

@ -0,0 +1,112 @@
{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.services.demostf-mover;
in {
options.services.demostf-mover = {
enable = mkEnableOption "Enables the demos mover service";
source = mkOption {
type = types.str;
description = "source directory";
};
target = mkOption {
type = types.str;
description = "target directory";
};
sourceBackend = mkOption {
type = types.str;
description = "source backend";
};
targetBackend = mkOption {
type = types.str;
description = "target backend";
};
keyFile = mkOption {
type = types.str;
description = "file containing the api key";
};
age = mkOption {
type = types.int;
default = 78894000;
description = "age of files to move in secconds";
};
logLevel = mkOption {
type = types.str;
default = "INFO";
description = "log level";
};
user = mkOption {
type = types.str;
description = "user that owns the demos";
};
interval = mkOption {
type = types.str;
default = "*:0/10";
description = "Interval to run the service";
};
package = mkOption {
type = types.package;
defaultText = literalExpression "pkgs.demostf-mover";
description = "package to use";
};
};
config = mkIf cfg.enable {
systemd.services.demostf-mover = {
description = "Move demos for demos.tf";
environment = {
SOURCE_ROOT = cfg.source;
TARGET_ROOT = cfg.target;
SOURCE_BACKEND = cfg.sourceBackend;
TARGET_BACKEND = cfg.targetBackend;
AGE = toString cfg.age;
RUST_LOG = cfg.logLevel;
};
serviceConfig = {
ExecStart = "${cfg.package}/bin/demostf-mover";
EnvironmentFile = cfg.keyFile;
ReadWritePaths = [cfg.source cfg.target];
Restart = "on-failure";
User = cfg.user;
PrivateTmp = true;
ProtectSystem = "strict";
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";
RestrictRealtime = true;
ProtectProc = "noaccess";
SystemCallFilter = ["@system-service" "~@resources" "~@privileged"];
IPAddressDeny = "localhost link-local multicast";
};
};
systemd.timers.demostf-mover = {
enable = true;
description = "Move demos for demos.tf";
wantedBy = ["multi-user.target"];
timerConfig = {
OnCalendar = "*:0/10";
};
};
};
}

3
overlay.nix Normal file
View file

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

18
package.nix Normal file
View file

@ -0,0 +1,18 @@
{
stdenv,
rustPlatform,
lib,
}: let
inherit (lib.sources) sourceByRegex;
src = sourceByRegex ./. ["Cargo.*" "(src)(/.*)?"];
in
rustPlatform.buildRustPackage rec {
pname = "demostf-mover";
version = "0.1.0";
inherit src;
cargoLock = {
lockFile = ./Cargo.lock;
};
}

View file

@ -25,17 +25,17 @@ pub enum Error {
async fn main() -> Result<(), MainError> {
tracing_subscriber::fmt::init();
let mut args: HashMap<_, _> = dotenv::vars().collect();
let mut args: HashMap<_, _> = dotenvy::vars().collect();
let source_root: PathBuf = args
.remove("SOURCE_ROOT")
.expect("no SOURCE_ROOT set")
.trim_end_matches("/")
.trim_end_matches('/')
.into();
let target_root: PathBuf = args
.remove("TARGET_ROOT")
.expect("no TARGET_ROOT set")
.trim_end_matches("/")
.trim_end_matches('/')
.into();
let api_key = args.remove("KEY").expect("no KEY set");
let source_backend = args
@ -96,7 +96,7 @@ async fn move_demo(
) -> MainResult {
if !source_path.is_file() {
warn!("source not found, re-downloading");
re_download(&client, &target_path, &demo).await?;
re_download(client, &target_path, demo).await?;
}
if target_path.is_file() {
warn!("target exists");
@ -136,8 +136,8 @@ async fn move_demo(
Ok(())
}
fn generate_path(basedir: &PathBuf, name: &str) -> PathBuf {
let mut path = basedir.clone();
fn generate_path(basedir: &Path, name: &str) -> PathBuf {
let mut path = basedir.to_path_buf();
path.push(&name[0..2]);
path.push(&name[2..4]);
path.push(name);
@ -159,7 +159,7 @@ fn hash<P: AsRef<Path>>(path: P) -> Result<[u8; 16], Error> {
}
let data = &buff[0..read];
hash.consume(&data);
hash.consume(data);
}
Ok(hash.compute().0)
@ -169,7 +169,7 @@ fn hash<P: AsRef<Path>>(path: P) -> Result<[u8; 16], Error> {
async fn re_download(client: &ApiClient, target: &Path, demo: &Demo) -> Result<(), Error> {
let mut data = Vec::with_capacity(demo.duration as usize / 60 * 1024);
timeout(Duration::from_secs(5 * 60), demo.save(&client, &mut data))
timeout(Duration::from_secs(5 * 60), demo.save(client, &mut data))
.await
.map_err(|_| Error::Timeout)??;