Skip to content

Commit

Permalink
nix: support reproducible builds
Browse files Browse the repository at this point in the history
Nix lets anyone build this glorious repo without even cloning it.
  • Loading branch information
numinit committed Dec 5, 2024
1 parent 181fb09 commit 416c437
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
target/
*.falsemetal
*.joeydecaio
/result
32 changes: 31 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,37 @@ The `HelloWorldMainLauncherClass` performs the following tasks:
- Java Development Kit (JDK) 8 or higher.
- A Java IDE (like IntelliJ IDEA, Eclipse, or NetBeans) or a command-line environment to compile and run the Java program.

## Installation
## Running with Nix

Nix is a way to reproducibly manage dependencies without using containers. Now everyone can run the same version of Nanowar Of Steel's glorious Hello World program.

1. First, [get Nix](https://nixos.org/).

2. Clone the repository:
```bash
git clone https://github.com/NanowarOfSteel/HelloWorld.git
```

3. Run `nix-shell` to drop into a shell with all the development dependencies like Maven and the JDK, or run `nix-build` to build the program.

4. Output is at `result/bin/HelloWorld`.

### Flakes

If you have [Flakes](https://wiki.nixos.org/wiki/Flakes) enabled via methods like the [Determinate Nix Installer](https://github.com/DeterminateSystems/nix-installer),
you can run this project without even cloning it:

1. Enable Flakes via the [wiki](https://wiki.nixos.org/wiki/Flakes) or the [Determinate Nix Installer](https://github.com/DeterminateSystems/nix-installer).

2. Build and run it:
```bash
nix run github:NanowarOfSteel/HelloWorld
```

With flakes, you can also use `nix develop` instead of `nix-shell` and `nix build` instead of `nix-build`.
`nix flake update` will update the version of nixpkgs used.

## Manual Installation

To set up and run this project locally, follow these steps:

Expand Down
47 changes: 47 additions & 0 deletions build.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
jre,
jdk,
makeWrapper,
maven,
}:

maven.buildMavenPackage {
pname = "it.nanowar.ofsteel.helloworld";
version = "0.0.1";
src = ./.;

mvnHash = "sha256-QVRwMC4FREgwojlX5JzTap2eg7HPFNgElIeCVycPOAI=";

buildInputs = [ jre ];
nativeBuildInputs = [ jdk makeWrapper maven ];

installPhase = ''
mkdir -p $out/bin $out/share/HelloWorld
jar="$(echo target/HelloWorld-*.jar)"
install -Dm644 "$jar" $out/share/HelloWorld
makeWrapper ${jre}/bin/java $out/bin/HelloWorld \
--add-flags "-cp $out/share/HelloWorld/$(basename -- "$jar")" \
--add-flags "it.nanowar.ofsteel.helloworld.HelloWorldMainLauncherClass"
'';

doInstallCheck = true;
installCheckPhase = ''
result="$({ $out/bin/HelloWorld || true; } 2>&1)"
echo "Result:" >&2
echo "--------" >&2
echo "$result" >&2
echo "--------" >&2
if [ "$(echo "$result" | grep 'Hello World!' | wc -l)" != 4 ]; then
echo "Incorrect number of hello world lines" >&2
exit 1
fi
if [ -z "$(echo "$result" | grep NullPointerException | grep joeyDeCaio)" ]; then
echo "Program should have failed with the correct error message" >&2
exit 1
fi
'';
}
13 changes: 13 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(import
(
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
nodeName = lock.nodes.root.inputs.flake-compat;
in
fetchTarball {
url = lock.nodes.${nodeName}.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.${nodeName}.locked.rev}.tar.gz";
sha256 = lock.nodes.${nodeName}.locked.narHash;
}
)
{ src = ./.; }
).defaultNix
14 changes: 14 additions & 0 deletions develop.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
mkShell,
HelloWorld,
cowsay
}:

mkShell {
inherit (HelloWorld) pname version;
inputsFrom = [ HelloWorld ];
buildInputs = [ cowsay ];
shellHook = ''
echo "nanowar of steel rulez" | cowsay -f dragon >&2
'';
}
73 changes: 73 additions & 0 deletions flake.lock

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

33 changes: 33 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
description = "https://www.youtube.com/watch?v=yup8gIXxWDU";
inputs = {
flake-parts.url = "github:hercules-ci/flake-parts";
flake-compat.url = "https://flakehub.com/f/edolstra/flake-compat/1.tar.gz";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11";
};
outputs =
inputs@{ flake-parts, ... }:
flake-parts.lib.mkFlake { inherit inputs; } {
systems = [
"x86_64-linux"
"aarch64-linux"
"aarch64-darwin"
"x86_64-darwin"
];
perSystem =
{
config,
self',
inputs',
pkgs,
system,
...
}:
rec {
packages.default = pkgs.callPackage ./build.nix { };
devShells.default = pkgs.callPackage ./develop.nix {
HelloWorld = packages.default;
};
};
};
}
13 changes: 13 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(import
(
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
nodeName = lock.nodes.root.inputs.flake-compat;
in
fetchTarball {
url = lock.nodes.${nodeName}.locked.url or "https://github.com/edolstra/flake-compat/archive/${lock.nodes.${nodeName}.locked.rev}.tar.gz";
sha256 = lock.nodes.${nodeName}.locked.narHash;
}
)
{ src = ./.; }
).shellNix

0 comments on commit 416c437

Please sign in to comment.