nasirhm/hello-rust-nix
{ "createdAt": "2020-12-01T17:09:36Z", "defaultBranch": "main", "description": "Hello World with Rust and Nix", "fullName": "nasirhm/hello-rust-nix", "homepage": null, "language": "Nix", "name": "hello-rust-nix", "pushedAt": "2020-12-02T11:00:45Z", "stargazersCount": 9, "topics": [], "updatedAt": "2024-09-04T09:47:12Z", "url": "https://github.com/nasirhm/hello-rust-nix"}Hello Rust with Nix
Section titled “Hello Rust with Nix”Rust Application:
Section titled “Rust Application:”A Rocket Application, exposing the following endpoints:
- ”/” - for a Simple Hello World
- “/hostinfo” - for getting the host information the binary is running on, it gets the following information in JSON.
- Hostname.
- Process ID.
- Uptime.
Building the Application:
Section titled “Building the Application:”Docker / Container:
Section titled “Docker / Container:”We have a Dockerfile available to build the project locally.
How it works:
Section titled “How it works:”- It uses,
rustlang/rust:nightly-slimto build the project. - It then uses,
ubuntu:18.04as a runtime for the built binary. - It initializes an environment variable as
ROCKET_PORTand sets it to8000to serve the project on port8000.
Building it:
Section titled “Building it:”$ docker build -t nasirhm/hello-nix-rust .Running the built container image:
Section titled “Running the built container image:”$ docker run --rm -it -p 8000:8000 nasirhm/hello-nix-rustIt’ll run the application in a Docker container and map port 8000 of host to the port 8000 of the container.
How it works:
Section titled “How it works:”- With
nix,lorri,direnvinstalled andlorri daemonrunning in the backgroud, when you’llcd .into the directory, It’ll create a nix-shell with Rust available in it. - It’ll fetch rust from,
mozilla/nixpkgs-mozillafromnightlychannel and version2020-11-01overlaid into it. - There are 2 nix configs for building the binary.
- One being:
helloworld.nix, which will do the following things:- Download naerksk
- Download every Rust crate our HTTP service depends on into the Nix store
- Run your program’s tests
- Build your dependencies into a Nix package
- Build your program with those dependencies
- Place a link to the result at
./result - The resultant binary will be in:
./result/bin/helloworld
- The other being:
default.nix, which will do the following things:- Do the stuff, done by
helloworld.nix. - nixpkgs provides
dockerToolswhich we will be using to create docker image out of Nix package. - It’ll create a docker image with
nasirhm/hello-nix-rustas name andlatestas a tag. - It’ll also set
ROCKET_PORTto 5000 to let Rocket know to run application on port 5000.
- Do the stuff, done by
- One being:
Building the Nix Package
Section titled “Building the Nix Package”To build the Nix Package.
$ nix-build helloworld.nixTo run:
$ ./result/bin/helloworldBuilding the Container image from dockerTools
Section titled “Building the Container image from dockerTools”To build the Container image:
$ nix-build default.nixIt’ll create a tarball containing the docker image information as the result of the Nix build.
To run the image with Docker, we first have to load it:
$ docker load -i resultand then run it using docker run:
$ docker run --rm -itp 8000:5000 nasirhm/hello-nix-rustIt’ll map port 8000 of the host with 5000 of the container.
NOTE: We can also use podman instead of docker as a container engine / runtime.
To Test:
Section titled “To Test:”To test the application, we can use cargo for it:
$ cargo testIt has the following testcases:
- For the
/endpoint:- It makes sure, HTTP reponse Status Ok 200 is returned.
- and, ensures, it returns
Hello, World!as a response.
- For the
/hostinfoendpoint:- It makes sure, HTTP response Status Ok 200 is returned.
- and, ensures, we get the correct / current hostname, process id and uptime (in secs).
- in JSON schema.
Credits
Section titled “Credits”Thank You, Xe for writing the following awesome articles from which I created this short project for learning more about Rust and Nix: