From 49d64805cae6b60f26f3710ccd3d9a680674a4d5 Mon Sep 17 00:00:00 2001 From: StepBroBD Date: Sun, 4 Jun 2023 09:57:28 -0600 Subject: [PATCH] add nix flake (#210) - add following functionality: - nix develop with direnv support - nix run and nix shell - nix fmt for flake.nix - and package overlay for https://github.com/NixOS/nixpkgs/pull/225502 - useful docs - https://stackoverflow.com/questions/53272197/how-do-i-override-the-libc-in-a-nix-package-to-be-musl - https://github.com/NixOS/nixpkgs/blob/dd3aca2d0b9dcb4888779bfb08805d79596607c9/pkgs/top-level/stage.nix#L136 - inspired by https://github.com/typst/typst/blob/main/flake.nix --- .envrc | 1 + .gitignore | 9 ++++++- README.md | 7 +++++ flake.lock | 59 +++++++++++++++++++++++++++++++++++++++++ flake.nix | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 .envrc create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index 5bb2fd7..76cbb0a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,10 @@ -/.idea +# Rust /target + +# Editor +/.idea /.vscode + +# Nix +/result +/.direnv diff --git a/README.md b/README.md index 059c7f2..0858123 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,13 @@ A pure [Rust](https://www.rust-lang.org/) CLI for [Crunchyroll](https://www.crun Check out the [releases](https://github.com/crunchy-labs/crunchy-cli/releases) tab and get the binary from the latest (pre-)release. +### ❄️ The nix way + +This requires [nix](https://nixos.org) and you'll probably need `--extra-experimental-features "nix-command flakes"` depending on your configurations. +```shell +$ nix github:crunchy-labs/crunchy-cli +``` + ### 🛠 Build it yourself Since we do not support every platform and architecture you may have to build the project yourself. diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..91bbf3d --- /dev/null +++ b/flake.lock @@ -0,0 +1,59 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1685866647, + "narHash": "sha256-4jKguNHY/edLYImB+uL8jKPL/vpfOvMmSlLAGfxSrnY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "a53a3bec10deef6e1cc1caba5bc60f53b959b1e8", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixpkgs-unstable", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "utils": "utils" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1685518550, + "narHash": "sha256-o2d0KcvaXzTrPRIo0kOLV0/QXHhDQ5DTi+OxcjO8xqY=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "a1720a10a6cfe8234c0e93907ffe81be440f4cef", + "type": "github" + }, + "original": { + "id": "flake-utils", + "type": "indirect" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..4a48dec --- /dev/null +++ b/flake.nix @@ -0,0 +1,78 @@ +{ + inputs = { + nixpkgs.url = "flake:nixpkgs/nixpkgs-unstable"; + utils.url = "flake:flake-utils"; + }; + + outputs = { self, nixpkgs, utils, ... }: utils.lib.eachSystem [ + "aarch64-darwin" + "x86_64-darwin" + "aarch64-linux" + "x86_64-linux" + ] + (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + # enable musl on Linux makes the build time 100x slower + # since it will trigger a toolchain rebuild + # if nixpkgs.legacyPackages.${system}.stdenv.hostPlatform.isLinux + # then nixpkgs.legacyPackages.${system}.pkgsMusl + # else nixpkgs.legacyPackages.${system}; + + crunchy-cli = pkgs.rustPlatform.buildRustPackage.override { stdenv = pkgs.clangStdenv; } rec { + pname = "crunchy-cli"; + inherit ((pkgs.lib.importTOML ./Cargo.toml).package) version; + + src = pkgs.lib.cleanSource ./.; + + cargoLock = { + lockFile = ./Cargo.lock; + allowBuiltinFetchGit = true; + }; + + nativeBuildInputs = [ + pkgs.pkg-config + ] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [ + pkgs.xcbuild + ]; + + buildInputs = [ + pkgs.openssl + ] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [ + pkgs.darwin.Security + ]; + }; + in + { + packages.default = crunchy-cli; + + overlays.default = _: prev: { + crunchy-cli = prev.crunchy-cli.override { }; + }; + + devShells.default = pkgs.mkShell { + packages = with pkgs; [ + cargo + clippy + rust-analyzer + rustc + rustfmt + ]; + + inputsFrom = builtins.attrValues self.packages.${system}; + + buildInputs = [ + pkgs.openssl + pkgs.libiconv + ] ++ pkgs.lib.optionals pkgs.stdenv.isDarwin [ + pkgs.darwin.apple_sdk.frameworks.CoreServices + pkgs.darwin.Security + ]; + + RUST_SRC_PATH = pkgs.rustPlatform.rustLibSrc; + }; + + formatter = pkgs.nixpkgs-fmt; + } + ); +}