Part2: "Hello GNU"
Using the Nix-Shell, this part will show how to compile a simple C program that runs in the home directory.
There are other ways to do this, so why use a nix-shell ?
- A Flake would work- but this method is very complex and requires a high bandwidth Internet connection, which I don't have at the moment.
- Gcc could be installed on the system permanently, plus any dependencies for the project.
- However this would make developer SBOM obligations more difficult
- Substitution of dependencies for testing or when problems arise is harder
Nix-shell: step 1, in a src/ directory
Create a hello.c file with the following contents
#include <stdio.h>
int main() {
printf("Hello, Gnu!\n");
return 0;
}
step 2
Create a shell.nix file with the following contents
with import <nixpkgs> {};
mkShell {
buildInputs = [ gcc ];
}
step 3
Run the following command
nix-shell --command 'gcc hello.c -o hello'
step 4
Test the program
./hello
And you should see:
Hello, Gnu!
Finish!
Project Files
.
├── pics
│ ├── helix.png
│ └── hello-world.png
└── src
├── hello
├── hello.c
├── main.fs
└── shell.nix