Skip to content

Commit

Permalink
Setup nix flake
Browse files Browse the repository at this point in the history
  • Loading branch information
bezbac committed Mar 14, 2024
1 parent 977e9b5 commit 82812e9
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 32 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.DS_Store
/target
/output
result
49 changes: 49 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Building the Dockerfile without having nix installed (inside docker)

1. Starting the Docker container

```
docker run -it --rm -v "$(pwd):/app" nixos/nix
```

2. Enable flakes

```
echo 'experimental-features = nix-command flakes' >> /etc/nix/nix.conf
```

3. Switch to app directory

```
cd /app
```

4. Build the binary

```
nix build .#bin
```

5. Copy the binary to the output folder

```
cp -r $(readlink -f result) output
```

6. Build the Docker image

```
nix build .#dockerImage
```

7. Copy the Docker image to the output folder

```
cp $(readlink -f result) output/docker-image.tar.gz
```

# Importing the nix-built image into docker

```
docker load -i output/docker-image.tar.gz
```
30 changes: 0 additions & 30 deletions Dockerfile

This file was deleted.

3 changes: 1 addition & 2 deletions examples/minimal/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ services:
principal:
container_name: principal
command: --port 8080 --threshold 10 --rate 10s --timeout 60s --container http-echo --debug
build:
context: ../../
image: principal:latest
ports:
- 8080:8080
volumes:
Expand Down
106 changes: 106 additions & 0 deletions flake.lock

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

54 changes: 54 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
flake-utils.url = "github:numtide/flake-utils";

rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs = {
nixpkgs.follows = "nixpkgs";
flake-utils.follows = "flake-utils";
};
};

crane = {
url = "github:ipetkov/crane";
inputs = { nixpkgs.follows = "nixpkgs"; };
};
};
outputs = { self, nixpkgs, flake-utils, rust-overlay, crane }:
flake-utils.lib.eachDefaultSystem (system:
let
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs { inherit system overlays; };
rustToolchain = pkgs.pkgsBuildHost.rust-bin.fromRustupToolchainFile
./rust-toolchain.toml;
craneLib = (crane.mkLib pkgs).overrideToolchain rustToolchain;
src = craneLib.cleanCargoSource ./.;
nativeBuildInputs = with pkgs; [ rustToolchain pkg-config ];
buildInputs = with pkgs; [ openssl libpcap ];
commonArgs = { inherit src buildInputs nativeBuildInputs; };
cargoArtifacts = craneLib.buildDepsOnly commonArgs;

bin = craneLib.buildPackage (commonArgs // {
inherit cargoArtifacts;

# Compile but don't run the tests
cargoTestExtraArgs = "--no-run";
});

dockerImage = pkgs.dockerTools.buildImage {
name = "principal";
tag = "latest";
copyToRoot = [ bin ];
config = { Entrypoint = [ "${bin}/bin/principal" ]; };
};
in
with pkgs; {
packages = {
inherit bin dockerImage;
default = bin;
};
devShells.default = mkShell { inputsFrom = [ bin ]; };
});
}
3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "stable"
components = ["rustfmt", "rustc-dev", "clippy"]

0 comments on commit 82812e9

Please sign in to comment.