himalaya/flake.nix

231 lines
8.1 KiB
Nix
Raw Normal View History

{
description = "CLI to manage emails";
inputs = {
2024-01-01 15:22:30 +00:00
nixpkgs.url = "github:nixos/nixpkgs/nixos-23.11";
gitignore = {
url = "github:hercules-ci/gitignore.nix";
inputs.nixpkgs.follows = "nixpkgs";
};
fenix = {
2024-04-16 19:26:00 +00:00
url = "github:soywod/fenix";
inputs.nixpkgs.follows = "nixpkgs";
};
naersk = {
url = "github:nix-community/naersk";
inputs.nixpkgs.follows = "nixpkgs";
};
2023-05-03 14:54:39 +00:00
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
};
outputs = { self, nixpkgs, gitignore, fenix, naersk, ... }:
let
2024-04-18 21:34:15 +00:00
inherit (nixpkgs) lib;
inherit (gitignore.lib) gitignoreSource;
2024-04-18 20:06:24 +00:00
crossSystems = {
2024-04-16 19:26:00 +00:00
x86_64-linux = {
x86_64-linux = {
rustTarget = "x86_64-unknown-linux-musl";
};
aarch64-linux = rec {
2024-04-16 19:26:00 +00:00
rustTarget = "aarch64-unknown-linux-musl";
2024-04-18 20:06:24 +00:00
runner = { pkgs, himalaya }: "${pkgs.qemu}/bin/qemu-aarch64 ${himalaya}";
mkPackage = { system, pkgs }: package:
2024-04-16 19:26:00 +00:00
let
inherit (mkPkgsCross system rustTarget) stdenv;
cc = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
in
package // {
2024-04-16 19:26:00 +00:00
TARGET_CC = cc;
CARGO_BUILD_RUSTFLAGS = package.CARGO_BUILD_RUSTFLAGS ++ [ "-Clinker=${cc}" ];
2024-04-16 19:26:00 +00:00
};
};
x86_64-windows = {
rustTarget = "x86_64-pc-windows-gnu";
2024-04-18 20:06:24 +00:00
runner = { pkgs, himalaya }:
let wine = pkgs.wine.override { wineBuild = "wine64"; };
2024-04-18 20:42:11 +00:00
in "${wine}/bin/wine64 ${himalaya}.exe";
mkPackage = { system, pkgs }: package:
2024-04-16 19:26:00 +00:00
let
inherit (pkgs.pkgsCross.mingwW64) stdenv windows;
2024-04-16 19:26:00 +00:00
cc = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc";
in
package // {
2024-04-16 19:26:00 +00:00
depsBuildBuild = [ stdenv.cc windows.pthreads ];
TARGET_CC = cc;
CARGO_BUILD_RUSTFLAGS = package.CARGO_BUILD_RUSTFLAGS ++ [ "-Clinker=${cc}" ];
2024-04-16 19:26:00 +00:00
};
};
};
aarch64-linux = {
aarch64-linux = {
rustTarget = "aarch64-unknown-linux-musl";
};
};
2024-04-16 19:26:00 +00:00
x86_64-darwin = {
x86_64-darwin = {
2024-04-16 19:26:00 +00:00
rustTarget = "x86_64-apple-darwin";
mkPackage = { pkgs, ... }: package:
let inherit (pkgs.darwin.apple_sdk.frameworks) AppKit Cocoa;
in package // {
2024-04-16 19:26:00 +00:00
buildInputs = [ Cocoa ];
NIX_LDFLAGS = "-F${AppKit}/Library/Frameworks -framework AppKit";
};
};
2024-04-19 15:04:02 +00:00
# FIXME: https://github.com/NixOS/nixpkgs/issues/273442
aarch64-darwin = {
rustTarget = "aarch64-apple-darwin";
runner = { pkgs, himalaya }: "${pkgs.qemu}/bin/qemu-aarch64 ${himalaya}";
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 = {
2024-04-16 19:26:00 +00:00
rustTarget = "aarch64-apple-darwin";
mkPackage = { pkgs, ... }: package:
let inherit (pkgs.darwin.apple_sdk.frameworks) AppKit Cocoa;
in package // {
2024-04-16 19:26:00 +00:00
buildInputs = [ Cocoa ];
NIX_LDFLAGS = "-F${AppKit}/Library/Frameworks -framework AppKit";
2024-04-16 19:26:00 +00:00
};
};
};
};
2024-04-19 15:04:02 +00:00
eachBuildSystem = lib.genAttrs (builtins.attrNames crossSystems);
2024-04-16 19:26:00 +00:00
mkPkgsCross = buildSystem: crossSystem: import nixpkgs {
system = buildSystem;
crossSystem.config = crossSystem;
};
2024-04-19 15:04:02 +00:00
mkToolchain = import ./rust-toolchain.nix fenix;
mkApp = { pkgs, buildSystem, targetSystem ? buildSystem }:
let
2024-04-19 18:44:03 +00:00
himalaya = lib.getExe self.packages.${buildSystem}.${targetSystem};
2024-04-19 20:11:16 +00:00
wrapper = crossSystems.${buildSystem}.${targetSystem}.runner or (_: himalaya) { inherit pkgs himalaya; };
2024-04-19 15:04:02 +00:00
program = lib.getExe (pkgs.writeShellScriptBin "himalaya" "${wrapper} $@");
2024-04-19 20:11:16 +00:00
app = { inherit program; type = "app"; };
in
app;
2024-04-19 15:04:02 +00:00
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; };
2024-04-19 20:11:16 +00:00
mkPackage = { pkgs, buildSystem, targetSystem ? buildSystem }:
let
targetConfig = crossSystems.${buildSystem}.${targetSystem};
toolchain = mkToolchain.fromTarget {
inherit pkgs buildSystem;
targetSystem = targetConfig.rustTarget;
};
rust = naersk.lib.${buildSystem}.override {
cargo = toolchain;
rustc = toolchain;
};
mkPackage' = targetConfig.mkPackage or (_: p: p);
himalaya = "./himalaya";
runner = targetConfig.runner or (_: himalaya) { inherit pkgs himalaya; };
package = mkPackage' { inherit pkgs; system = buildSystem; } {
name = "himalaya";
src = gitignoreSource ./.;
strictDeps = true;
doCheck = false;
auditable = false;
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/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
${pkgs.zip}/bin/zip -r himalaya.zip himalaya* share
mv share ../
mv himalaya.tgz himalaya.zip ../
'';
};
in
rust.buildPackage package;
2024-04-18 20:06:24 +00:00
mkPackages = buildSystem:
let
2024-04-18 20:06:24 +00:00
pkgs = import nixpkgs { system = buildSystem; };
2024-04-19 20:11:16 +00:00
mkPackage' = targetSystem: _: mkPackage { inherit pkgs buildSystem targetSystem; };
defaultPackage = mkPackage { inherit pkgs buildSystem; };
packages = builtins.mapAttrs mkPackage' crossSystems.${buildSystem};
in
2024-04-19 15:04:02 +00:00
packages // { default = defaultPackage; };
2024-04-19 15:04:02 +00:00
mkDevShells = buildSystem:
2024-01-01 15:22:30 +00:00
let
2024-04-18 20:06:24 +00:00
pkgs = import nixpkgs { system = buildSystem; };
2024-04-19 15:04:02 +00:00
rust-toolchain = mkToolchain.fromFile { inherit buildSystem; };
defaultShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [ pkg-config ];
buildInputs = with pkgs; [
# Nix
nixd
nixpkgs-fmt
2024-04-19 15:04:02 +00:00
# Rust
rust-toolchain
cargo-watch
2024-04-16 19:26:00 +00:00
2024-04-19 15:04:02 +00:00
# Email env
gnupg
gpgme
msmtp
notmuch
];
};
in
{ default = defaultShell; };
2024-04-18 21:34:15 +00:00
in
{
2024-04-19 15:04:02 +00:00
apps = eachBuildSystem mkApps;
packages = eachBuildSystem mkPackages;
devShells = eachBuildSystem mkDevShells;
};
}