forked from TwentyTwoHW/portal-software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
174 lines (153 loc) · 7.34 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
{
description = "Software for the Portal hardware wallet";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
rust-overlay.url = "github:oxalica/rust-overlay";
flake-utils.url = "github:numtide/flake-utils";
crane = {
url = "github:ipetkov/crane";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { self, nixpkgs, rust-overlay, flake-utils, crane, ... }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
inherit system overlays;
config.android_sdk.accept_license = true;
config.allowUnfree = true;
};
rustVersion = "1.76.0";
getRust =
# fullAndroid implies withAndroid
{ fullAndroid ? false, withAndroid ? fullAndroid, withIos ? false, withEmbedded ? false, nightly ? false }:
let
rs = if nightly then pkgs.rust-bin.nightly."2024-01-31" else pkgs.rust-bin.stable.${rustVersion};
in (rs.default.override {
extensions = [
"rust-src" # for rust-analyzer
];
targets = []
++ pkgs.lib.optionals withEmbedded ["thumbv7em-none-eabihf"]
++ pkgs.lib.optionals withAndroid ["x86_64-linux-android" "aarch64-linux-android"]
++ pkgs.lib.optionals fullAndroid ["i686-linux-android" "armv7-linux-androideabi"]
++ pkgs.lib.optionals withIos [ "aarch64-apple-ios-sim" "x86_64-apple-ios" "aarch64-apple-ios" ];
});
getCrane = data: (crane.mkLib pkgs).overrideToolchain (getRust data);
# hal is still using miniscript v9.0.1 which doesn't compile on >= 1.71
halRustPlatform = pkgs.makeRustPlatform {
cargo = pkgs.rust-bin.stable."1.70.0".minimal;
rustc = pkgs.rust-bin.stable."1.70.0".minimal;
};
rust = getRust { withEmbedded = false; };
rustPlatform = pkgs.makeRustPlatform {
cargo = rust;
rustc = rust;
};
android = {
buildToolsVersion = "30.0.3";
cmakeVersion = "3.6.4111459";
};
androidShellHook = ''
# Add cmake to the path.
cmake_root="$(echo "$ANDROID_SDK_ROOT/cmake/${android.cmakeVersion}"*/)"
export PATH="$cmake_root/bin:$PATH"
# Write out local.properties for Android Studio.
cat <<EOF > ./sdk/libportal-android/local.properties
# This file was automatically generated by nix-shell.
sdk.dir=$ANDROID_SDK_ROOT
ndk.dir=$ANDROID_NDK_ROOT
cmake.dir=$cmake_root
EOF
'';
androidComposition = pkgs.androidenv.composeAndroidPackages {
buildToolsVersions = [ android.buildToolsVersion ];
platformVersions = [ "33" ];
includeNDK = true;
ndkVersion = "23.1.7779620";
cmakeVersions = [ android.cmakeVersion ];
};
defaultDeps = with pkgs; [ cmake SDL2 fltk pango rust-analyzer pkg-config libusb ];
embeddedDeps = with pkgs; [ probe-rs gcc-arm-embedded gdb openocd clang lld (getRust { withEmbedded = true; nightly = true; }) ];
androidDeps = with pkgs; [ cargo-ndk jdk gnupg (getRust { fullAndroid = true; }) ];
iosDeps = with pkgs; [ (getRust { withIos = true; }) ];
in
rec {
devShells.default = pkgs.mkShell {
buildInputs = defaultDeps ++ [ rust ];
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
};
devShells.embedded = pkgs.mkShell {
buildInputs = defaultDeps ++ embeddedDeps ++ [ packages.hal packages.qemuPortal ];
LIBCLANG_PATH = "${pkgs.llvmPackages.libclang.lib}/lib";
CC_thumbv7em_none_eabihf = "clang-18";
CFLAGS_thumbv7em_none_eabihf = "-flto -fno-data-sections -fno-function-sections -fno-PIC -fno-stack-protector --target=thumbv7em-none-eabihf -mcpu=cortex-m4 -mthumb -I${pkgs.clang_17}/resource-root/include/ -I${pkgs.gcc-arm-embedded}/arm-none-eabi/include";
};
devShells.android = pkgs.mkShell rec {
buildInputs = defaultDeps ++ androidDeps;
ANDROID_SDK_ROOT = "${androidComposition.androidsdk}/libexec/android-sdk";
ANDROID_NDK_ROOT = "${ANDROID_SDK_ROOT}/ndk-bundle";
# Ensures that we don't have to use a FHS env by using the nix store's aapt2.
GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${ANDROID_SDK_ROOT}/build-tools/${android.buildToolsVersion}/aapt2";
shellHook = androidShellHook;
};
devShells.ios = pkgs.mkShell {
buildInputs = defaultDeps ++ iosDeps;
shellHook = ''
${
pkgs.lib.optionalString pkgs.stdenv.isDarwin ''export PATH="/usr/bin:$PATH"''
}
'';
};
packages.hal = halRustPlatform.buildRustPackage rec {
pname = "hal";
version = "0.9.3";
src = pkgs.fetchFromGitHub {
owner = "stevenroose";
repo = pname;
rev = "v${version}";
hash = "sha256-QOp7YM/R8mhDVbSaABGjRqqqHW288UYWHxezz5dUAwU=";
};
cargoHash = "sha256-/+ld3zfyCpylEPUoGoOCBHiski2lle8QW+/zoW/PgmM=";
meta = with pkgs.lib; {
description = "the Bitcoin companion";
homepage = "https://github.com/stevenroose/hal";
license = licenses.unlicense;
maintainers = [];
};
};
packages.qemuPortal = let
qemu' = (pkgs.qemu.override { hostCpuTargets = ["arm-softmmu"]; }).overrideAttrs(old: rec {
version = "v9.0.0-portal.4";
src = pkgs.fetchurl {
url = "https://github.com/TwentyTwoHW/qemu-portal/releases/download/${version}/qemu-${version}.tar.xz";
hash = "sha256-88q0pmb2F4DKPfnCz3/FTkibQD7ACXMslZfMTc5F2/k=";
};
sourceRoot = ".";
});
in
pkgs.writeShellScriptBin "qemu-system-arm" ''
exec ${qemu'}/bin/qemu-system-arm "$@"
'';
packages.emulator = pkgs.callPackage ./emulator { inherit pkgs; craneLib = getCrane { withEmbedded = false; }; };
packages.gui-simulator = pkgs.callPackage ./gui { inherit pkgs; craneLib = getCrane { withEmbedded = false; }; };
packages.model = pkgs.callPackage ./model { inherit pkgs; craneLib = getCrane { withEmbedded = false; }; };
packages.sdk = pkgs.callPackage ./sdk { inherit pkgs; craneLib = getCrane { withEmbedded = false; }; };
packages.firmware-development = pkgs.callPackage ./firmware rec {
inherit pkgs;
rustToolchain = getRust { withEmbedded = true; nightly = true; };
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
production = false;
};
packages.firmware-production = packages.firmware;
packages.firmware = pkgs.callPackage ./firmware rec {
inherit pkgs;
rustToolchain = getRust { withEmbedded = true; nightly = true; };
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
};
packages.docker.emulatorImage = pkgs.callPackage ./docker/emulator.nix { inherit pkgs packages; };
packages.docker.devEnvironment = pkgs.callPackage ./docker/embedded-dev.nix { inherit pkgs packages devShells getRust; };
}
);
}