diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..1d953f4 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use nix diff --git a/.gitignore b/.gitignore index b31d8f0..e61649e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,12 @@ /target .ignore .vscode -fig +.direnv +**/fig **/*.png !filegram-web/images/*.png **/*.blb **/*.key **/dist +result Cargo.lock \ No newline at end of file diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..2c956bc --- /dev/null +++ b/default.nix @@ -0,0 +1,9 @@ +(import ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; } +) { + src = ./.; +}).defaultNix \ No newline at end of file diff --git a/filegram-web/Cargo.toml b/filegram-web/Cargo.toml index 8978b06..0debbee 100644 --- a/filegram-web/Cargo.toml +++ b/filegram-web/Cargo.toml @@ -10,7 +10,7 @@ filegram = { path = "../filegram" } js-sys = "0.3.68" image = { version = "0.24.9", default-features = false } web-sys = { version = "0.3.67", default-features = false } -wasm-bindgen = { default-features = false, version = "0.2.91" } +wasm-bindgen = { version = "0.2.91", default-features = false } yew = { version = "0.21.0", features = ["csr"] } gloo-file = "0.3.0" gloo-utils = { version = "0.2.0", default-features = false } diff --git a/filegram-web/src/decode.rs b/filegram-web/src/decode.rs index d55290c..665d526 100644 --- a/filegram-web/src/decode.rs +++ b/filegram-web/src/decode.rs @@ -175,9 +175,7 @@ impl DecodeComponent { fn download_file(file_name: &str, url: &ObjectUrl) { let download_element = document().create_element("a").unwrap(); - download_element - .set_attribute("href", url) - .unwrap(); + download_element.set_attribute("href", url).unwrap(); download_element .set_attribute("download", file_name) .unwrap(); diff --git a/filegram-web/src/encode.rs b/filegram-web/src/encode.rs index 5f7a6df..4a17365 100644 --- a/filegram-web/src/encode.rs +++ b/filegram-web/src/encode.rs @@ -160,9 +160,7 @@ impl EncodeComponent { fn download_file(file_name: &str, url: &ObjectUrl) { let download_element = document().create_element("a").unwrap(); - download_element - .set_attribute("href", url) - .unwrap(); + download_element.set_attribute("href", url).unwrap(); download_element .set_attribute("download", file_name) .unwrap(); diff --git a/filegram/src/padding.rs b/filegram/src/padding.rs index ad88faa..cb2c98d 100644 --- a/filegram/src/padding.rs +++ b/filegram/src/padding.rs @@ -13,7 +13,7 @@ pub fn pad_block(data: Vec) -> Vec { block.to_vec() } -pub fn unpad_block(data: &Vec) -> Result, UnpadError> { +pub fn unpad_block(data: &[u8]) -> Result, UnpadError> { let mut block: GenericArray = GenericArray::clone_from_slice(&[0u8; BUFFER_SIZE]); let data_len = data.len(); block[..data_len].copy_from_slice(data); diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..ce87f2c --- /dev/null +++ b/flake.lock @@ -0,0 +1,140 @@ +{ + "nodes": { + "crane": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1711681752, + "narHash": "sha256-LEg6/dmEFxx6Ygti5DO9MOhGNpyB7zdxdWtzv/FCTXk=", + "owner": "ipetkov", + "repo": "crane", + "rev": "ada0fb4dcce4561acb1eb17c59b7306d9d4a95f3", + "type": "github" + }, + "original": { + "owner": "ipetkov", + "repo": "crane", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1711715736, + "narHash": "sha256-9slQ609YqT9bT/MNX9+5k5jltL9zgpn36DpFB7TkttM=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "807c549feabce7eddbf259dbdcec9e0600a0660d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-for-wasm-bindgen": { + "locked": { + "lastModified": 1711715736, + "narHash": "sha256-9slQ609YqT9bT/MNX9+5k5jltL9zgpn36DpFB7TkttM=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "807c549feabce7eddbf259dbdcec9e0600a0660d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "807c549feabce7eddbf259dbdcec9e0600a0660d", + "type": "github" + } + }, + "root": { + "inputs": { + "crane": "crane", + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "nixpkgs-for-wasm-bindgen": "nixpkgs-for-wasm-bindgen", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": [ + "flake-utils" + ], + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1711851236, + "narHash": "sha256-EJ03x3N9ihhonAttkaCrqxb0djDq3URCuDpmVPbNZhA=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "f258266af947599e8069df1c2e933189270f143a", + "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..d43548d --- /dev/null +++ b/flake.nix @@ -0,0 +1,161 @@ +{ + description = "Filegram project flake."; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + + # The version of wasm-bindgen-cli needs to match the version in Cargo.lock + # Update this to include the version you need + nixpkgs-for-wasm-bindgen.url = "github:NixOS/nixpkgs/807c549feabce7eddbf259dbdcec9e0600a0660d"; + + crane = { + url = "github:ipetkov/crane"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + + flake-utils.url = "github:numtide/flake-utils"; + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + + rust-overlay = { + url = "github:oxalica/rust-overlay"; + inputs = { + nixpkgs.follows = "nixpkgs"; + flake-utils.follows = "flake-utils"; + }; + }; + }; + + outputs = { self, nixpkgs, crane, flake-utils, flake-compat, rust-overlay, nixpkgs-for-wasm-bindgen, ... }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ (import rust-overlay) ]; + }; + + inherit (pkgs) lib; + + rustToolchain = pkgs.rust-bin.stable.latest.default.override { + # Set the build targets supported by the toolchain, + # wasm32-unknown-unknown is required for trunk. + targets = [ "wasm32-unknown-unknown" ]; + }; + craneLib = ((crane.mkLib pkgs).overrideToolchain rustToolchain).overrideScope (_final: _prev: { + # The version of wasm-bindgen-cli needs to match the version in Cargo.lock. You + # can unpin this if your nixpkgs commit contains the appropriate wasm-bindgen-cli version + inherit (import nixpkgs-for-wasm-bindgen { inherit system; }) wasm-bindgen-cli; + }); + + # When filtering sources, we want to allow assets other than .rs files + src = lib.cleanSourceWith { + src = ./.; # The original, unfiltered source + filter = path: type: + (lib.hasSuffix "\.html" path) || + (lib.hasSuffix "\.scss" path) || + (lib.hasSuffix "\.css" path) || + # Example of a folder for images, icons, etc + (lib.hasInfix "/assets/" path) || + # Default filter from crane (allow .rs files) + (craneLib.filterCargoSources path type) + ; + }; + + + # Arguments to be used by both the client and the server + # When building a workspace with crane, it's a good idea + # to set "pname" and "version". + commonArgs = { + inherit src; + pname = "filegram"; + version = "0.2.0"; + strictDeps = true; + }; + + # Native packages + + nativeArgs = commonArgs // { + pname = "filegram-native"; + }; + + # Build *just* the cargo dependencies, so we can reuse + # all of that work (e.g. via cachix) when running in CI + cargoArtifacts = craneLib.buildDepsOnly nativeArgs; + + # Filegram CLI + filegram-cli = craneLib.buildPackage (nativeArgs // { + inherit cargoArtifacts; + pname = "filegram-cli"; + cargoExtraArgs = "--package=filegram-cli"; + }); + + # Wasm packages + + # it's not possible to build the cli on the + # wasm32 target, so we only build the web app. + wasmArgs = commonArgs // { + pname = "filegram-wasm"; + cargoExtraArgs = "--package=filegram-web"; + CARGO_BUILD_TARGET = "wasm32-unknown-unknown"; + }; + + cargoArtifactsWasm = craneLib.buildDepsOnly (wasmArgs // { + doCheck = false; + }); + + # Build the frontend of the application. + # This derivation is a directory you can put on a webserver. + filegram-web = craneLib.buildTrunkPackage (wasmArgs // { + pname = "filegram-web"; + cargoArtifacts = cargoArtifactsWasm; + trunkIndexPath = "filegram-web/index.html"; + wasm-bindgen-cli = pkgs.wasm-bindgen-cli.override { + version = "0.2.91"; + }; + }); + in + { + checks = { + # Build the crate as part of `nix flake check` for convenience + inherit filegram-cli filegram-web; + + # Run clippy (and deny all warnings) on the crate source, + # again, reusing the dependency artifacts from above. + # + # Note that this is done as a separate derivation so that + # we can block the CI if there are issues here, but not + # prevent downstream consumers from building our crate by itself. + filegram-clippy = craneLib.cargoClippy (commonArgs // { + inherit cargoArtifacts; + cargoClippyExtraArgs = "--all-targets -- --deny warnings"; + }); + + # Check formatting + filegram-fmt = craneLib.cargoFmt commonArgs; + }; + + packages = rec { + cli = filegram-cli; + web = filegram-web; + default = cli; + }; + + apps.default = flake-utils.lib.mkApp { + name = "filegram-cli"; + drv = filegram-cli; + }; + + devShells.default = craneLib.devShell { + # Inherit inputs from checks. + checks = self.checks.${system}; + + # Extra inputs can be added here; cargo and rustc are provided by default. + packages = [ + pkgs.trunk + ]; + }; + }); +} + diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..9c43e06 --- /dev/null +++ b/shell.nix @@ -0,0 +1,9 @@ +(import ( + let + lock = builtins.fromJSON (builtins.readFile ./flake.lock); + in fetchTarball { + url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz"; + sha256 = lock.nodes.flake-compat.locked.narHash; } +) { + src = ./.; +}).shellNix \ No newline at end of file