From f5ae10de78d5fc5142a962dfe309b7c00533735c Mon Sep 17 00:00:00 2001 From: a-kenji Date: Sun, 6 Mar 2022 14:28:54 +0100 Subject: [PATCH] add(nix): add crate2nix (#1181) * add(nix): add crate2nix To have a better experience on ci, due to improved caching of dependencies. Also organize file hierarchy slightly. * ci(nix): manually check a default system Is needed because `nix flake check` accidentally tries to build IFD. --- .github/workflows/nix.yml | 49 +++++++++++++-- flake.lock | 17 +++++ flake.nix | 127 +------------------------------------ nix/crate2nix.nix | 30 +++++++++ nix/default.nix | 129 ++++++++++++++++++++++++++++++++++++++ nix/devShell.nix | 11 ++++ 6 files changed, 235 insertions(+), 128 deletions(-) create mode 100644 nix/crate2nix.nix create mode 100644 nix/default.nix create mode 100644 nix/devShell.nix diff --git a/.github/workflows/nix.yml b/.github/workflows/nix.yml index f6ef3e36..c78b831b 100644 --- a/.github/workflows/nix.yml +++ b/.github/workflows/nix.yml @@ -9,11 +9,33 @@ on: - main jobs: - nix: + #check: + #runs-on: ubuntu-latest + #name: "flake check" + #environment: cachix + #timeout-minutes: 5 + #steps: + #- uses: actions/checkout@v3 + #with: + ## Nix Flakes doesn't work on shallow clones + #fetch-depth: 0 + #- uses: cachix/install-nix-action@v16 + #with: + #extra_nix_config: | + #experimental-features = nix-command flakes + #fetch-depth: 0 + #- uses: cachix/cachix-action@v10 + #with: + #name: zellij + ## If you chose API tokens for write access OR if you have a private cache + #authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' + #- run: nix flake check --print-build-logs --show-trace + + build: runs-on: ubuntu-latest - name: nix-build + name: "build" environment: cachix - timeout-minutes: 15 + timeout-minutes: 25 steps: - uses: actions/checkout@v3 with: @@ -29,6 +51,25 @@ jobs: name: zellij # If you chose API tokens for write access OR if you have a private cache authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' - - run: nix flake check --print-build-logs --show-trace - run: nix build + develop: + runs-on: ubuntu-latest + name: develop + environment: cachix + timeout-minutes: 10 + steps: + - uses: actions/checkout@v3 + with: + # Nix Flakes doesn't work on shallow clones + fetch-depth: 0 + - uses: cachix/install-nix-action@v16 + with: + extra_nix_config: | + experimental-features = nix-command flakes + fetch-depth: 0 + - uses: cachix/cachix-action@v10 + with: + name: zellij + # If you chose API tokens for write access OR if you have a private cache + authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' - run: nix develop --profile devShell diff --git a/flake.lock b/flake.lock index 44af65bb..de190c8c 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,21 @@ { "nodes": { + "crate2nix": { + "flake": false, + "locked": { + "lastModified": 1646322090, + "narHash": "sha256-Jtqd5Ory+1LgMMTY0tNJKd/U2mOiPRd/oYnuyTHE08o=", + "owner": "kolloch", + "repo": "crate2nix", + "rev": "c4a479172ebafdd3baf36aa274b2168b4fd42f40", + "type": "github" + }, + "original": { + "owner": "kolloch", + "repo": "crate2nix", + "type": "github" + } + }, "flake-compat": { "flake": false, "locked": { @@ -49,6 +65,7 @@ }, "root": { "inputs": { + "crate2nix": "crate2nix", "flake-compat": "flake-compat", "flake-utils": "flake-utils", "nixpkgs": "nixpkgs", diff --git a/flake.nix b/flake.nix index 1650eefd..6e01933d 100644 --- a/flake.nix +++ b/flake.nix @@ -10,130 +10,9 @@ rust-overlay.inputs.flake-utils.follows = "flake-utils"; flake-compat.url = "github:edolstra/flake-compat"; flake-compat.flake = false; + crate2nix.url = "github:kolloch/crate2nix"; + crate2nix.flake = false; }; - outputs = { self, rust-overlay, nixpkgs, flake-utils, flake-compat }: - flake-utils.lib.eachSystem [ - "aarch64-linux" - "aarch64-darwin" - "i686-linux" - "x86_64-darwin" - "x86_64-linux" - ] - (system: - let - overlays = [ (import rust-overlay) ]; - - pkgs = import nixpkgs { inherit system overlays; }; - - name = "zellij"; - pname = name; - root = toString ./.; - - ignoreSource = [ ".git" "target" "example" ]; - - src = pkgs.nix-gitignore.gitignoreSource ignoreSource root; - - rustToolchainToml = pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain; - cargoLock = { - lockFile = (builtins.path { path = ./Cargo.lock; name = "Cargo.lock"; }); - }; - cargo = rustToolchainToml; - rustc = rustToolchainToml; - - #env - RUST_BACKTRACE = 1; - - buildInputs = [ - rustToolchainToml - - # in order to run tests - pkgs.openssl - ]; - - nativeBuildInputs = [ - # generates manpages - pkgs.mandown - - pkgs.installShellFiles - pkgs.copyDesktopItems - - # for openssl/openssl-sys - pkgs.pkg-config - ]; - - devInputs = [ - pkgs.cargo-make - pkgs.rust-analyzer - pkgs.nixpkgs-fmt - - # optimizes wasm binaries - pkgs.binaryen - - # used for snapshotting the e2e tests - pkgs.cargo-insta - ]; - - in - rec { - - packages.zellij = - (pkgs.makeRustPlatform { inherit cargo rustc; }).buildRustPackage { - inherit src name cargoLock buildInputs nativeBuildInputs; - - preCheck = '' - HOME=$TMPDIR - ''; - - postInstall = '' - mandown ./docs/MANPAGE.md > ./zellij.1 - installManPage ./zellij.1 - - # explicit behavior - $out/bin/zellij setup --generate-completion bash > ./completions.bash - installShellCompletion --bash --name ${pname}.bash ./completions.bash - $out/bin/zellij setup --generate-completion fish > ./completions.fish - installShellCompletion --fish --name ${pname}.fish ./completions.fish - $out/bin/zellij setup --generate-completion zsh > ./completions.zsh - installShellCompletion --zsh --name _${pname} ./completions.zsh - - install -Dm644 ./assets/logo.png $out/share/icons/hicolor/scalable/apps/zellij.png - - copyDesktopItems - ''; - - desktopItems = [ - (pkgs.makeDesktopItem { - type = "Application"; - inherit name; - desktopName = "zellij"; - terminal = true; - genericName = "Terminal multiplexer"; - comment = "Manage your terminal applications"; - exec = "zellij"; - icon = "zellij"; - categories = [ "ConsoleOnly;System" ]; - }) - ]; - - meta = with pkgs.lib; { - homepage = "https://github.com/zellij-org/zellij/"; - description = "A terminal workspace with batteries included"; - license = [ licenses.mit ]; - }; - }; - - defaultPackage = packages.zellij; - - # nix run - apps.zellij = flake-utils.lib.mkApp { drv = packages.zellij; }; - defaultApp = apps.zellij; - - devShell = pkgs.mkShell { - name = "zellij-dev"; - inherit buildInputs RUST_BACKTRACE; - nativeBuildInputs = nativeBuildInputs ++ devInputs; - }; - - }); + outputs = { ... } @ args: import ./nix args; } diff --git a/nix/crate2nix.nix b/nix/crate2nix.nix new file mode 100644 index 00000000..14c1e749 --- /dev/null +++ b/nix/crate2nix.nix @@ -0,0 +1,30 @@ +{ pkgs +, crate2nix +, name +, src +, postInstall +, desktopItems +, meta +}: + +let + inherit (import "${crate2nix}/tools.nix" { inherit pkgs; }) + generatedCargoNix; + + project = import + (generatedCargoNix { + inherit name src; + }) + { + inherit pkgs; + buildRustCrateForPkgs = pkgs: + pkgs.buildRustCrate.override { + defaultCrateOverrides = pkgs.defaultCrateOverrides // { + inherit postInstall desktopItems meta; + # Crate dependency overrides go here + }; + }; + }; + +in +project.workspaceMembers.zellij.build diff --git a/nix/default.nix b/nix/default.nix new file mode 100644 index 00000000..a863810a --- /dev/null +++ b/nix/default.nix @@ -0,0 +1,129 @@ +{ self +, nixpkgs +, rust-overlay +, flake-utils +, flake-compat +, crate2nix +}: +flake-utils.lib.eachSystem [ + "aarch64-linux" + "aarch64-darwin" + "i686-linux" + "x86_64-darwin" + "x86_64-linux" +] + (system: + let + overlays = [ (import rust-overlay) ]; + + pkgs = import nixpkgs { inherit system overlays; }; + + name = "zellij"; + pname = name; + root = toString ../.; + + ignoreSource = [ ".git" "target" "example" ]; + + src = pkgs.nix-gitignore.gitignoreSource ignoreSource root; + + rustToolchainToml = pkgs.rust-bin.fromRustupToolchainFile ../rust-toolchain; + cargoLock = { + lockFile = (builtins.path { path = ../Cargo.lock; name = "Cargo.lock"; }); + }; + cargo = rustToolchainToml; + rustc = rustToolchainToml; + + buildInputs = [ + rustToolchainToml + + # in order to run tests + pkgs.openssl + ]; + + nativeBuildInputs = [ + # generates manpages + pkgs.mandown + + pkgs.installShellFiles + pkgs.copyDesktopItems + + # for openssl/openssl-sys + pkgs.pkg-config + ]; + + devInputs = [ + pkgs.cargo-make + pkgs.rust-analyzer + pkgs.nixpkgs-fmt + + # optimizes wasm binaries + pkgs.binaryen + + # used for snapshotting the e2e tests + pkgs.cargo-insta + ]; + + postInstall = '' + mandown ./docs/MANPAGE.md > ./zellij.1 + installManPage ./zellij.1 + + # explicit behavior + $out/bin/zellij setup --generate-completion bash > ./completions.bash + installShellCompletion --bash --name ${pname}.bash ./completions.bash + $out/bin/zellij setup --generate-completion fish > ./completions.fish + installShellCompletion --fish --name ${pname}.fish ./completions.fish + $out/bin/zellij setup --generate-completion zsh > ./completions.zsh + installShellCompletion --zsh --name _${pname} ./completions.zsh + + install -Dm644 ./assets/logo.png $out/share/icons/hicolor/scalable/apps/zellij.png + + copyDesktopItems + ''; + + desktopItems = [ + (pkgs.makeDesktopItem { + type = "Application"; + inherit name; + desktopName = "zellij"; + terminal = true; + genericName = "Terminal multiplexer"; + comment = "Manage your terminal applications"; + exec = "zellij"; + icon = "zellij"; + categories = [ "ConsoleOnly;System" ]; + }) + ]; + meta = with pkgs.lib; { + homepage = "https://github.com/zellij-org/zellij/"; + description = "A terminal workspace with batteries included"; + license = [ licenses.mit ]; + }; + in + rec { + + # crate2nix - better incremental builds, but uses ifd + packages.zellij = pkgs.callPackage ./crate2nix.nix { + inherit crate2nix name src desktopItems postInstall meta; + }; + + # native nixpkgs support - keep supported + packages.zellij-native = + (pkgs.makeRustPlatform { inherit cargo rustc; }).buildRustPackage { + inherit src name cargoLock + buildInputs nativeBuildInputs + postInstall desktopItems meta; + }; + + defaultPackage = packages.zellij; + + # nix run + apps.zellij = flake-utils.lib.mkApp { drv = packages.zellij; }; + defaultApp = apps.zellij; + + + devShell = pkgs.callPackage ./devShell.nix { + inherit buildInputs; + nativeBuildInputs = nativeBuildInputs ++ devInputs; + }; + + }) diff --git a/nix/devShell.nix b/nix/devShell.nix new file mode 100644 index 00000000..86850e70 --- /dev/null +++ b/nix/devShell.nix @@ -0,0 +1,11 @@ +{ mkShell +, buildInputs +, nativeBuildInputs +}: + +mkShell { + name = "zj-dev-env"; + inherit buildInputs nativeBuildInputs; + ### Environment Variables + RUST_BACKTRACE = 1; +}