Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nix config #65

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use nix
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/target
.ignore
.vscode
fig
.direnv
**/fig
**/*.png
!filegram-web/images/*.png
**/*.blb
**/*.key
**/dist
result
Cargo.lock
9 changes: 9 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion filegram-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 1 addition & 3 deletions filegram-web/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 1 addition & 3 deletions filegram-web/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion filegram/src/padding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn pad_block(data: Vec<u8>) -> Vec<u8> {
block.to_vec()
}

pub fn unpad_block(data: &Vec<u8>) -> Result<Vec<u8>, UnpadError> {
pub fn unpad_block(data: &[u8]) -> Result<Vec<u8>, UnpadError> {
let mut block: GenericArray<u8, U255> = GenericArray::clone_from_slice(&[0u8; BUFFER_SIZE]);
let data_len = data.len();
block[..data_len].copy_from_slice(data);
Expand Down
140 changes: 140 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

161 changes: 161 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -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
];
};
});
}

9 changes: 9 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -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