Skip to content

Commit

Permalink
fix nix flake
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Sep 19, 2024
1 parent 4fd776f commit 5ad4a52
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 108 deletions.
14 changes: 7 additions & 7 deletions flake.lock

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

236 changes: 139 additions & 97 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
description = "CLI to synchronize, backup and restore emails";

inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";
nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05";
gitignore = {
url = "github:hercules-ci/gitignore.nix";
inputs.nixpkgs.follows = "nixpkgs";
Expand All @@ -25,165 +25,207 @@

outputs = { self, nixpkgs, gitignore, fenix, naersk, ... }:
let
inherit (nixpkgs) lib;
inherit (gitignore.lib) gitignoreSource;

staticRustFlags = [ "-Ctarget-feature=+crt-static" ];

# Map of map matching supported Nix build systems with Rust
# cross target systems.
crossBuildTargets = {
crossSystems = {
x86_64-linux = {
x86_64-linux = {
rustTarget = "x86_64-unknown-linux-musl";
override = { ... }: { };
};

arm64-linux = rec {
aarch64-linux = rec {
rustTarget = "aarch64-unknown-linux-musl";
override = { system, pkgs }:
runner = { pkgs, neverest }: "${pkgs.qemu}/bin/qemu-aarch64 ${neverest}";
mkPackage = { system, pkgs }: package:
let
inherit (mkPkgsCross system rustTarget) stdenv;
cc = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc"; in
rec {
cc = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
in
package // {
TARGET_CC = cc;
CARGO_BUILD_RUSTFLAGS = staticRustFlags ++ [ "-Clinker=${cc}" ];
postInstall = mkPostInstall {
inherit pkgs;
bin = "${pkgs.qemu}/bin/qemu-aarch64 ./neverest";
};
CARGO_BUILD_RUSTFLAGS = package.CARGO_BUILD_RUSTFLAGS ++ [ "-Clinker=${cc}" ];
};
};

x86_64-windows = {
rustTarget = "x86_64-pc-windows-gnu";
override = { system, pkgs }:
runner = { pkgs, neverest }:
let wine = pkgs.wine.override { wineBuild = "wine64"; };
in "${wine}/bin/wine64 ${neverest}.exe";
mkPackage = { system, pkgs }: package:
let
inherit (pkgs) pkgsCross zip;
inherit (pkgsCross.mingwW64) stdenv windows;
inherit (pkgs.pkgsCross.mingwW64) stdenv windows;
cc = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
wine = pkgs.wine.override { wineBuild = "wine64"; };
postInstall = mkPostInstall {
inherit pkgs;
bin = "${wine}/bin/wine64 ./neverest.exe";
};
in
{
package // {
depsBuildBuild = [ stdenv.cc windows.pthreads ];
TARGET_CC = cc;
CARGO_BUILD_RUSTFLAGS = staticRustFlags ++ [ "-Clinker=${cc}" ];
postInstall = ''
export WINEPREFIX="$(mktemp -d)"
${postInstall}
'';
CARGO_BUILD_RUSTFLAGS = package.CARGO_BUILD_RUSTFLAGS ++ [ "-Clinker=${cc}" ];
};
};
};

aarch64-linux = {
aarch64-linux = {
rustTarget = "aarch64-unknown-linux-musl";
};
};

x86_64-darwin = {
x86_64-macos = {
x86_64-darwin = {
rustTarget = "x86_64-apple-darwin";
override = { pkgs, ... }:
let inherit (pkgs.darwin.apple_sdk.frameworks) AppKit Cocoa; in
{
mkPackage = { pkgs, ... }: package:
let inherit (pkgs.darwin.apple_sdk.frameworks) AppKit Cocoa;
in package // {
buildInputs = [ Cocoa ];
NIX_LDFLAGS = "-F${AppKit}/Library/Frameworks -framework AppKit";
};
};

# FIXME: https://github.com/NixOS/nixpkgs/issues/273442
aarch64-darwin = {
rustTarget = "aarch64-apple-darwin";
runner = { pkgs, neverest }: "${pkgs.qemu}/bin/qemu-aarch64 ${neverest}";
mkPackage = { system, pkgs }: package:
let
inherit ((mkPkgsCross system "aarch64-darwin").pkgsStatic) stdenv darwin;
inherit (darwin.apple_sdk.frameworks) AppKit Cocoa;
cc = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
in
package // {
buildInputs = [ Cocoa ];
NIX_LDFLAGS = "-F${AppKit}/Library/Frameworks -framework AppKit";
TARGET_CC = cc;
CARGO_BUILD_RUSTFLAGS = package.CARGO_BUILD_RUSTFLAGS ++ [ "-Clinker=${cc}" ];
};
};
};

aarch64-darwin = {
aarch64-darwin = {
rustTarget = "aarch64-apple-darwin";
mkPackage = { pkgs, ... }: package:
let inherit (pkgs.darwin.apple_sdk.frameworks) AppKit Cocoa;
in package // {
buildInputs = [ Cocoa ];
NIX_LDFLAGS = "-F${AppKit}/Library/Frameworks -framework AppKit";
};
};
};
};

mkToolchain = import ./rust-toolchain.nix fenix;
eachBuildSystem = lib.genAttrs (builtins.attrNames crossSystems);

mkPkgsCross = buildSystem: crossSystem: import nixpkgs {
system = buildSystem;
crossSystem.config = crossSystem;
};

mkPostInstall = { pkgs, bin ? "./neverest" }: with pkgs; ''
cd $out/bin
mkdir -p {man,completions}
${bin} man ./man
${bin} completion bash > ./completions/neverest.bash
${bin} completion elvish > ./completions/neverest.elvish
${bin} completion fish > ./completions/neverest.fish
${bin} completion powershell > ./completions/neverest.powershell
${bin} completion zsh > ./completions/neverest.zsh
tar -czf neverest.tgz neverest* man completions
${zip}/bin/zip -r neverest.zip neverest* man completions
'';

mkDevShells = buildPlatform:
mkToolchain = import ./rust-toolchain.nix fenix;

mkApp = { pkgs, buildSystem, targetSystem ? buildSystem }:
let
pkgs = import nixpkgs { system = buildPlatform; };
rust-toolchain = mkToolchain.fromFile { system = buildPlatform; };
neverest = lib.getExe self.packages.${buildSystem}.${targetSystem};
wrapper = crossSystems.${buildSystem}.${targetSystem}.runner or (_: neverest) { inherit pkgs neverest; };
program = lib.getExe (pkgs.writeShellScriptBin "neverest" "${wrapper} $@");
app = { inherit program; type = "app"; };
in
{
default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ pkg-config ];
buildInputs = with pkgs; [
# Nix
nil
nixpkgs-fmt
app;

# Rust
rust-toolchain
cargo-watch

# Notmuch
notmuch
];
};
};
mkApps = buildSystem:
let
pkgs = import nixpkgs { system = buildSystem; };
mkApp' = targetSystem: _: mkApp { inherit pkgs buildSystem targetSystem; };
defaultApp = mkApp { inherit pkgs buildSystem; };
apps = builtins.mapAttrs mkApp' crossSystems.${buildSystem};
in
apps // { default = defaultApp; };

mkPackage = pkgs: buildPlatform: targetPlatform: package:
mkPackage = { pkgs, buildSystem, targetSystem ? buildSystem }:
let
targetConfig = crossSystems.${buildSystem}.${targetSystem};
toolchain = mkToolchain.fromTarget {
inherit pkgs buildPlatform targetPlatform;
inherit pkgs buildSystem;
targetSystem = targetConfig.rustTarget;
};
naersk' = naersk.lib.${buildPlatform}.override {
rust = naersk.lib.${buildSystem}.override {
cargo = toolchain;
rustc = toolchain;
};
package' = {
mkPackage' = targetConfig.mkPackage or (_: p: p);
neverest = "./neverest";
runner = targetConfig.runner or (_: neverest) { inherit pkgs neverest; };
package = mkPackage' { inherit pkgs; system = buildSystem; } {
name = "neverest";
src = gitignoreSource ./.;
strictDeps = true;
doCheck = false;
auditable = false;
strictDeps = true;
CARGO_BUILD_TARGET = targetPlatform;
CARGO_BUILD_RUSTFLAGS = staticRustFlags;
postInstall = mkPostInstall { inherit pkgs; };
} // package;
nativeBuildInputs = with pkgs; [ pkg-config ];
CARGO_BUILD_TARGET = targetConfig.rustTarget;
CARGO_BUILD_RUSTFLAGS = [ "-Ctarget-feature=+crt-static" ];
postInstall = ''
export WINEPREFIX="$(mktemp -d)"
mkdir -p $out/bin/share/{applications,completions,man,services}
cp assets/neverest.desktop $out/bin/share/applications/
cp assets/neverest-watch@.service $out/bin/share/services/
cd $out/bin
${runner} man ./share/man
${runner} completion bash > ./share/completions/neverest.bash
${runner} completion elvish > ./share/completions/neverest.elvish
${runner} completion fish > ./share/completions/neverest.fish
${runner} completion powershell > ./share/completions/neverest.powershell
${runner} completion zsh > ./share/completions/neverest.zsh
tar -czf neverest.tgz neverest* share
${pkgs.zip}/bin/zip -r neverest.zip neverest* share
mv share ../
mv neverest.tgz neverest.zip ../
'';
};
in
naersk'.buildPackage package';
rust.buildPackage package;

mkPackages = system:
mkPackages = buildSystem:
let
pkgs = import nixpkgs { inherit system; };
mkPackage' = target: package: mkPackage pkgs system package.rustTarget (package.override { inherit system pkgs; });
pkgs = import nixpkgs { system = buildSystem; };
mkPackage' = targetSystem: _: mkPackage { inherit pkgs buildSystem targetSystem; };
defaultPackage = mkPackage { inherit pkgs buildSystem; };
packages = builtins.mapAttrs mkPackage' crossSystems.${buildSystem};
in
builtins.mapAttrs mkPackage' crossBuildTargets.${system};
packages // { default = defaultPackage; };

mkApp = drv:
let exePath = drv.passthru.exePath or "/bin/neverest"; in
{
type = "app";
program = "${drv}${exePath}";
};

mkApps = buildPlatform:
mkDevShells = buildSystem:
let
pkgs = import nixpkgs { system = buildPlatform; };
mkApp' = target: package: mkApp self.packages.${buildPlatform}.${target};
pkgs = import nixpkgs { system = buildSystem; };
rust-toolchain = mkToolchain.fromFile { inherit buildSystem; };
defaultShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ pkg-config ];
buildInputs = with pkgs; [
# Nix
nixd
nixpkgs-fmt

# Rust
rust-toolchain
cargo-watch

# Email env
gnupg
gpgme
notmuch
];
};
in
builtins.mapAttrs mkApp' crossBuildTargets.${buildPlatform};
{ default = defaultShell; };

supportedSystems = builtins.attrNames crossBuildTargets;
mapSupportedSystem = nixpkgs.lib.genAttrs supportedSystems;
in
{
apps = mapSupportedSystem mkApps;
packages = mapSupportedSystem mkPackages;
devShells = mapSupportedSystem mkDevShells;
apps = eachBuildSystem mkApps;
packages = eachBuildSystem mkPackages;
devShells = eachBuildSystem mkDevShells;
};
}
8 changes: 4 additions & 4 deletions rust-toolchain.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ let
sha256 = "+syqAd2kX8KVa8/U2gz3blIQTTsYYt3U63xBWaGOSc8=";
in
{
fromFile = { system }: fenix.packages.${system}.fromToolchainFile {
fromFile = { buildSystem }: fenix.packages.${buildSystem}.fromToolchainFile {
inherit file sha256;
};

fromTarget = { pkgs, buildPlatform, targetPlatform }:
fromTarget = { pkgs, buildSystem, targetSystem }:
let
name = (pkgs.lib.importTOML file).toolchain.channel;
fenixPackage = fenix.packages.${buildPlatform};
fenixPackage = fenix.packages.${buildSystem};
toolchain = fenixPackage.fromToolchainName { inherit name sha256; };
targetToolchain = fenixPackage.targets.${targetPlatform}.fromToolchainName { inherit name sha256; };
targetToolchain = fenixPackage.targets.${targetSystem}.fromToolchainName { inherit name sha256; };
in
fenixPackage.combine [
toolchain.rustc
Expand Down

0 comments on commit 5ad4a52

Please sign in to comment.