Skip to content

Commit

Permalink
Improve examples/ subfolder with cross-compilation and more docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cottand committed Aug 30, 2024
1 parent ac2152c commit 2a98cf7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 11 deletions.
31 changes: 30 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,33 @@ To see the overall plumbing to use the aws-nitro-util flake, see [flake.nix](./f
To see examples for specific EIFs, see the individual package definitions:

- Booting an enclave with a shell script only: [`withShellScript.nix`](./withShellScript.nix)
- Booting an enclave with your own, compiled-from-source kernel: [`bringYourOwnKernel.nix`](./bringYourOwnKernel.nix)
- Booting an enclave with your own, compiled-from-source kernel: [`bringYourOwnKernel.nix`](./bringYourOwnKernel.nix)

## Building the examples

**To show what examples can be built**

```bash
nix flake show
```

**To compile `shellScriptEif` for your current architecture:**
```bash
nix build .#shellScriptEif
```
Note this will produce an `aarch64-linux` EIF if you are running it in an ARM Mac.

Assuming you have a linux [remote builder](https://nix.dev/manual/nix/2.18/advanced-topics/distributed-builds) available,
**to compile EIFs natively for `x86_64-linux` on an ARM Mac:**

```bash
nix build .#packages.x86_64-linux.shellScriptEif
```

If you do not have remote builders, you can always try to cross-compile. Keep in mind this requires all dependencies
of your EIF to be cross-compiled too (which is tricky for bash scripts). **To cross-compile an EIF from your local system
to `x86_64-linux`:**

```bash
nix build .#x86_64-linux-shellScriptEif
```
15 changes: 9 additions & 6 deletions examples/flake.lock

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

23 changes: 21 additions & 2 deletions examples/flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";

nitro-util.url = "path:../";
nitro-util.url = "github:monzo/aws-nitro-util";
nitro-util.inputs.nixpkgs.follows = "nixpkgs";

flake-utils.url = "github:numtide/flake-utils";
Expand All @@ -14,7 +14,7 @@
in
{
packages = {

# the EIFs below will be for your machine's architecture
shellScriptEif = pkgs.callPackage ./withShellScript.nix {
inherit nitro;
};
Expand All @@ -28,6 +28,25 @@
inherit nitro;
};

# the EIFs below will be for the architecture in the package name,
# even if you build from a different machine
x86_64-linux-crossCompiledEif =
let
crossArch = "x86_64";
crossPkgs = import nixpkgs { inherit system; crossSystem = "${crossArch}-linux"; };
in
crossPkgs.callPackage ./withCrossCompilation.nix {
inherit crossArch nitro;
};

aarch64-linux-crossCompiledEif =
let
crossArch = "aarch64";
crossPkgs = import nixpkgs { inherit system; crossSystem = "${crossArch}-linux"; };
in
crossPkgs.callPackage ./withCrossCompilation.nix {
inherit crossArch nitro;
};
};
}));
}
28 changes: 28 additions & 0 deletions examples/withCrossCompilation.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{ buildEnv
, hello
, nitro # when you call this function pass `nitro-util.lib.${system}` here
, crossArch
}:
nitro.buildEif {
arch = crossArch;
kernel = nitro.blobs.${crossArch}.kernel;
kernelConfig = nitro.blobs.${crossArch}.kernelConfig;

name = "eif-hello-world";

nsmKo = nitro.blobs.${crossArch}.nsmKo;

copyToRoot = buildEnv {
name = "image-root";
# the image passed here must be a Nix derivation that can be cross-compiled
# we did not use a shell script here because that is hard for GNU coreutils
paths = [ hello ];
pathsToLink = [ "/bin" ];
};

entrypoint = ''
/bin/hello
'';

env = "";
}
2 changes: 0 additions & 2 deletions examples/withShellScript.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
}:
let
myScript = writeShellScriptBin "hello" ''
# note busybox can be used for building EIFs but only on Linux
# so remove this line if you are building an EIF on MacOS
export PATH="$PATH:${busybox}/bin"
while true;
Expand Down

0 comments on commit 2a98cf7

Please sign in to comment.