From 1db02076f3ece3d6d5eb604a07bea6c73666dfb3 Mon Sep 17 00:00:00 2001 From: NotNite Date: Fri, 28 Apr 2023 18:31:51 +0000 Subject: [PATCH] nix --- Cargo.lock | 7 +++ Cargo.toml | 7 +-- README.md | 2 +- flake.lock | 141 +++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 129 ++++++++++++++++++++++++++++++++++++++++++++ sqlx-data.json | 97 ++++++++++++++++++++++++++++++++++ upload.sh | 8 +-- 7 files changed, 383 insertions(+), 8 deletions(-) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 sqlx-data.json diff --git a/Cargo.lock b/Cargo.lock index 4e766fa..9a33cb9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -228,6 +228,9 @@ name = "either" version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +dependencies = [ + "serde", +] [[package]] name = "encoding_rs" @@ -1087,6 +1090,7 @@ dependencies = [ "percent-encoding", "rustls", "rustls-pemfile", + "serde", "sha2", "smallvec", "sqlformat", @@ -1107,9 +1111,12 @@ dependencies = [ "dotenvy", "either", "heck", + "hex", "once_cell", "proc-macro2", "quote", + "serde", + "serde_json", "sha2", "sqlx-core", "sqlx-rt", diff --git a/Cargo.toml b/Cargo.toml index 2f7fd1f..00c366f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,10 +1,11 @@ [package] name = "u" +description = "blazingly-fast super-concurrent webscale file server" +authors = ["NotNite "] +license = "MIT" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] anyhow = "1.0.69" async-trait = "0.1.65" @@ -14,7 +15,7 @@ hyper = "0.14.24" mime_guess = "2.0.4" nanoid = "0.4.0" serde = { version = "1.0.152", features = ["derive"] } -sqlx = { version = "0.6.2", features = ["sqlite", "runtime-tokio-rustls"] } +sqlx = { version = "0.6.2", features = ["sqlite", "runtime-tokio-rustls", "offline"] } tokio = { version = "1.26.0", features = ["full"] } tokio-stream = { version = "0.1.12", features = ["net"] } tower = "0.4.13" diff --git a/README.md b/README.md index be0dde5..58edc0f 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ requirements: ```sh export U_API_KEY="..." -export U_HOST="https://.../" +export U_HOST="https://..." export U_REVOCATION_KEY_PATH="$HOME/.u_revocation_keys" ./upload.sh /path/to/file diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..1c42b75 --- /dev/null +++ b/flake.lock @@ -0,0 +1,141 @@ +{ + "nodes": { + "crane": { + "inputs": { + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": [ + "nixpkgs" + ], + "rust-overlay": "rust-overlay" + }, + "locked": { + "lastModified": 1681680516, + "narHash": "sha256-EB8Adaeg4zgcYDJn9sR6UMjN/OHdIiMMK19+3LmmXQY=", + "owner": "ipetkov", + "repo": "crane", + "rev": "54b63c8eae4c50172cb50b612946ff1d2bc1c75c", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1673956053, + "narHash": "sha256-4gtG9iQuiKITOjNQQeQIpoIB6b16fm+504Ch3sNKLd8=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "35bb57c0c8d8b62bbfd284272c928ceb64ddbde9", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1678901627, + "narHash": "sha256-U02riOqrKKzwjsxc/400XnElV+UtPUQWpANPlyazjH0=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "93a2b84fc4b70d9e089d029deacc3583435c2ed6", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1681202837, + "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "cfacdce06f30d2b68473a46042957675eebb3401", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1682566018, + "narHash": "sha256-HPzPRFiy2o/7k7mtnwfM1E6NVZHiFbPdmYCMoIpkHO4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "8e3b64db39f2aaa14b35ee5376bd6a2e707cadc2", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "crane", + "flake-utils" + ], + "nixpkgs": [ + "crane", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1680488274, + "narHash": "sha256-0vYMrZDdokVmPQQXtFpnqA2wEgCCUXf5a3dDuDVshn0=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "7ec2ff598a172c6e8584457167575b3a1a5d80d8", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..2706707 --- /dev/null +++ b/flake.nix @@ -0,0 +1,129 @@ +{ + 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 <> $U_REVOCATION_KEY_PATH +echo "$image_id $image_revocation_key" >> "$U_REVOCATION_KEY_PATH" -image_url="https://u.n2.pm/i/$image_id.$extension" +image_url="$U_HOST/i/$image_id.$extension" echo "$image_url" echo "$image_url" | xclip -selection clipboard