From 772f689edae525b9a44adfed92d03bc06c99ab9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Sat, 23 Nov 2024 12:33:47 +0100 Subject: [PATCH] test ci --- .github/workflows/pre-release.yml | 29 +++++------ build.rs | 2 + cross-systems.nix | 11 +++++ default.nix | 82 ++++++++++++++++++++++++++----- package.nix | 71 ++++++++++++++++++++++++++ rust-toolchain.nix | 10 ++-- shell.nix | 26 +++++----- 7 files changed, 185 insertions(+), 46 deletions(-) create mode 100644 cross-systems.nix create mode 100644 package.nix diff --git a/.github/workflows/pre-release.yml b/.github/workflows/pre-release.yml index 4bbbf82b..540bbdd5 100644 --- a/.github/workflows/pre-release.yml +++ b/.github/workflows/pre-release.yml @@ -11,20 +11,15 @@ jobs: matrix: include: - os: ubuntu-latest - host: x86_64-linux - target: x86_64-linux + target: null - os: ubuntu-latest - host: x86_64-linux - target: aarch64-linux + target: aarch64-unknown-linux-musl - os: ubuntu-latest - host: x86_64-linux - target: x86_64-windows + target: x86_64-pc-windows-gnu - os: macos-13 - host: x86_64-darwin - target: x86_64-darwin + target: null - os: macos-14 - host: aarch64-darwin - target: aarch64-darwin + target: null steps: - name: Checkout code uses: actions/checkout@v4 @@ -33,7 +28,7 @@ jobs: with: nix_path: nixpkgs=channel:nixos-24.05 enable_kvm: true - extra_nix_config: "experimental-features = nix-command flakes" + # extra_nix_config: "experimental-features = nix-command flakes" - name: Cache Nix store uses: cachix/cachix-action@v15 with: @@ -41,13 +36,13 @@ jobs: authToken: '${{ secrets.CACHIX_AUTH_TOKEN }}' extraPullNames: nix-community - name: Build release + if: ${{ matrix.target }} != null run: | - nix build -L --expr " - (builtins.getFlake \"git+file://${PWD}?shallow=1&rev=$(git rev-parse HEAD)\") - .outputs.packages.${{ matrix.host }}.${{ matrix.target }}.overrideAttrs { - GIT_DESCRIBE = \"$(git describe --all --always --dirty)\"; - }" - nix run -L .#${{ matrix.target }} -- --version + nix-build --arg '{ config = "${{ matrix.target }}"; }' + - name: Build release + if: ${{ matrix.target }} == null + run: | + nix-build - name: Upload release artifacts uses: actions/upload-artifact@v4 with: diff --git a/build.rs b/build.rs index 48f4c2cb..f8d87eb4 100644 --- a/build.rs +++ b/build.rs @@ -74,6 +74,8 @@ fn git_envs() { // skip the process if the current directory is not a git // repository (for example, from a nix build root jail) let Ok(git) = Repository::open(".") else { + println!("cargo::rustc-env=GIT_DESCRIBE=unknown"); + println!("cargo::rustc-env=GIT_REV=unknown"); return; }; diff --git a/cross-systems.nix b/cross-systems.nix new file mode 100644 index 00000000..eda5a252 --- /dev/null +++ b/cross-systems.nix @@ -0,0 +1,11 @@ +{ + aarch64-unknown-linux-musl = { + runner = { qemu, ... }: "${qemu}/bin/qemu-aarch64 ./himalaya"; + }; + + x86_64-pc-windows-gnu = { + runner = { wine, ... }: + let wine64 = wine.override { wineBuild = "wine64"; }; + in "${wine64}/bin/wine64 ./himalaya.exe"; + }; +} diff --git a/default.nix b/default.nix index 7b821b27..3f39912e 100644 --- a/default.nix +++ b/default.nix @@ -1,12 +1,70 @@ -# This file exists for legacy Nix installs (nix-build & nix-env) -# https://nixos.wiki/wiki/Flakes#Using_flakes_project_from_a_legacy_Nix -# You generally do *not* have to modify this ever. -(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 +# https://nixos.org/manual/nixpkgs/stable/#sec-cross-usage +{ crossSystem ? null }: + +let + crossBuildEnabled = !isNull crossSystem; + crossSystems = import ./cross-systems.nix; + + buildPkgs = import { }; + hostPkgs = if crossBuildEnabled then import { inherit crossSystem; } else buildPkgs; + + inherit (hostPkgs.stdenv) buildPlatform hostPlatform; + + runner = if crossBuildEnabled then crossSystems.${hostPlatform.config}.runner buildPkgs else "./himalaya"; + + fenix = import (fetchTarball "https://github.com/soywod/fenix/archive/main.tar.gz") { }; + + mkToolchain = import ./rust-toolchain.nix fenix; + + rustToolchain = mkToolchain.fromTarget { + pkgs = hostPkgs; + targetSystem = buildPlatform.config; + }; + + rustPlatform = hostPkgs.makeRustPlatform { + rustc = rustToolchain; + cargo = rustToolchain; + }; + + himalaya = import ./package.nix { + inherit rustPlatform; + lib = hostPkgs.lib; + fetchFromGitHub = hostPkgs.fetchFromGitHub; + pkg-config = hostPkgs.pkg-config; + darwin = hostPkgs.darwin; + installShellFiles = false; + installShellCompletions = false; + installManPages = false; + notmuch = hostPkgs.notmuch; + gpgme = hostPkgs.gpgme; + stdenv = hostPkgs.stdenv; + }; +in + +himalaya.overrideAttrs (drv: { + version = "1.0.0"; + postInstall = '' + mkdir -p $out/bin/share/{applications,completions,man,services} + cp assets/himalaya.desktop $out/bin/share/applications/ + cp assets/himalaya-watch@.service $out/bin/share/services/ + + cd $out/bin + ${runner} man ./share/man + ${runner} completion bash > ./share/completions/himalaya.bash + ${runner} completion elvish > ./share/completions/himalaya.elvish + ${runner} completion fish > ./share/completions/himalaya.fish + ${runner} completion powershell > ./share/completions/himalaya.powershell + ${runner} completion zsh > ./share/completions/himalaya.zsh + + tar -czf himalaya.tgz himalaya* share + mv himalaya.tgz ../ + + ${hostPkgs.zip}/bin/zip -r himalaya.zip himalaya* share + mv himalaya.zip ../ + ''; + src = hostPkgs.nix-gitignore.gitignoreSource [ ] ./.; + cargoDeps = rustPlatform.importCargoLock { + lockFile = ./Cargo.lock; + allowBuiltinFetchGit = true; + }; +}) diff --git a/package.nix b/package.nix new file mode 100644 index 00000000..d06070a9 --- /dev/null +++ b/package.nix @@ -0,0 +1,71 @@ +{ lib +, rustPlatform +, fetchFromGitHub +, stdenv +, pkg-config +, darwin +, installShellFiles +, installShellCompletions ? stdenv.buildPlatform.canExecute stdenv.hostPlatform +, installManPages ? stdenv.buildPlatform.canExecute stdenv.hostPlatform +, notmuch +, gpgme +, buildNoDefaultFeatures ? false +, buildFeatures ? [ ] +, cargoLock ? null +}: + +rustPlatform.buildRustPackage rec { + inherit buildNoDefaultFeatures buildFeatures cargoLock; + + pname = "himalaya"; + version = "1.0.0-beta.4"; + + src = fetchFromGitHub { + owner = "soywod"; + repo = "himalaya"; + rev = "v${version}"; + hash = "sha256-NrWBg0sjaz/uLsNs8/T4MkUgHOUvAWRix1O5usKsw6o="; + }; + + cargoHash = "sha256-YS8IamapvmdrOPptQh2Ef9Yold0IK1XIeGs0kDIQ5b8="; + + NIX_BUILD_CORES = 4; + CARGO_TARGET_LINKER = "${stdenv.cc.targetPrefix}cc"; + CARGO_BUILD_RUSTFLAGS = [ "-Ctarget-feature=+crt-static" ]; + + doCheck = false; + cargoTestFlags = [ + # Only run lib tests (unit tests) + # All other tests are integration tests which should not be run with Nix build + "--lib" + ]; + + nativeBuildInputs = [ ] + ++ lib.optional (builtins.elem "pgp-gpg" buildFeatures) pkg-config + ++ lib.optional (installManPages || installShellCompletions) installShellFiles; + + buildInputs = [ ] + ++ lib.optionals stdenv.hostPlatform.isDarwin (with darwin.apple_sdk.frameworks; [ AppKit Cocoa Security ]) + ++ lib.optional (builtins.elem "notmuch" buildFeatures) notmuch + ++ lib.optional (builtins.elem "pgp-gpg" buildFeatures) gpgme; + + postInstall = lib.optionalString installManPages '' + mkdir -p $out/man + $out/bin/himalaya man $out/man + installManPage $out/man/* + '' + lib.optionalString installShellCompletions '' + installShellCompletion --cmd himalaya \ + --bash <($out/bin/himalaya completion bash) \ + --fish <($out/bin/himalaya completion fish) \ + --zsh <($out/bin/himalaya completion zsh) + ''; + + meta = { + description = "CLI to manage emails"; + mainProgram = "himalaya"; + homepage = "https://github.com/pimalaya/himalaya/"; + changelog = "https://github.com/soywod/himalaya/blob/v${version}/CHANGELOG.md"; + license = lib.licenses.mit; + maintainers = with lib.maintainers; [ soywod toastal yanganto ]; + }; +} diff --git a/rust-toolchain.nix b/rust-toolchain.nix index 57140c22..dd589f40 100644 --- a/rust-toolchain.nix +++ b/rust-toolchain.nix @@ -4,19 +4,19 @@ let file = ./rust-toolchain.toml; sha256 = "+syqAd2kX8KVa8/U2gz3blIQTTsYYt3U63xBWaGOSc8="; in + { fromFile = { buildSystem }: fenix.packages.${buildSystem}.fromToolchainFile { inherit file sha256; }; - fromTarget = { pkgs, buildSystem, targetSystem }: + fromTarget = { pkgs, targetSystem }: let name = (pkgs.lib.importTOML file).toolchain.channel; - fenixPackage = fenix.packages.${buildSystem}; - toolchain = fenixPackage.fromToolchainName { inherit name sha256; }; - targetToolchain = fenixPackage.targets.${targetSystem}.fromToolchainName { inherit name sha256; }; + toolchain = fenix.fromToolchainName { inherit name sha256; }; + targetToolchain = fenix.targets.${targetSystem}.fromToolchainName { inherit name sha256; }; in - fenixPackage.combine [ + fenix.combine [ toolchain.rustc toolchain.cargo targetToolchain.rust-std diff --git a/shell.nix b/shell.nix index a277e589..a8435481 100644 --- a/shell.nix +++ b/shell.nix @@ -1,12 +1,14 @@ -# This file exists for legacy nix-shell -# https://nixos.wiki/wiki/Flakes#Using_flakes_project_from_a_legacy_Nix -# You generally do *not* have to modify this ever. -(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 +{ pkgs ? import { + overlays = [ + (import (fetchTarball "https://github.com/oxalica/rust-overlay/archive/master.tar.gz")) + ]; + } +}: + +let + package = import ./default.nix { inherit pkgs; }; +in + +pkgs.mkShell { + inputsFrom = [ package ]; +}