diff --git a/Documentation/LinuxDevelopmentSetupUsingNix.md b/Documentation/LinuxDevelopmentSetupUsingNix.md new file mode 100644 index 00000000..aab90db2 --- /dev/null +++ b/Documentation/LinuxDevelopmentSetupUsingNix.md @@ -0,0 +1,64 @@ +# Development Environment Setup using Nix or Nix Flakes on Linux x86_64 +[Nix flakes](https://nixos.wiki/wiki/Flakes) can be used to provide version controlled reproducible and cross-platform development environments. The key files are: +- `flake.nix`: Defines the flake inputs and outputs, including development shells. +- `shell.nix`: This file defines the dependencies and additionally adds support for the Impure `nix-shell` method. This is used by the flake to create the dev environment. +- `flake.lock`: Locks the versions of inputs for reproducibility. +--- +## Prerequisites +- [Nix](https://nixos.org/download.html) the package manager or NixOs installed on Linux (x86_64-linux) +- Optional: flakes support enabled. +--- +## Using the Development Shell +You have two primary ways to enter the development shell with Nix: +### 1. Using `nix develop` (flake-native command) +This is the recommended way if you have Nix with flakes support. Flake guarantee the versions of the dependencies and can be controlled through `flake.nix` and `flake.lock`. +``` +nix develop +``` +This will open a shell with all dependencies and environment configured as per the `flake.nix` for (`x86_64-linux`) systems only at this time. + +--- +### 2. Using `nix-shell` (that's why shell.nix is a separate file) +If you want to use traditional `nix-shell` tooling which uses the nixpkgs version of your system: +``` +nix-shell +``` +This will drop you into the shell environment defined in `shell.nix`. Note that this is not flake-native method and does not use the locked nixpkgs in `flake.lock` so exact versions of the dependancies is not guaranteed. + +--- +## What’s inside the dev shell? +- The environment variables and packages configured in `shell.nix` will be available. +- The package set (`pkgs`) used aligns with the versions locked in `flake.lock` to ensure reproducibility. + +--- +## Example Workflow using flakes +``` +# Navigate to the project root folder which contains the flake.nix, flake.lock and shell.nix files. +cd /home/user/dev/Libation +# Enter the flake development shell (Linux x86_64) +nix develop +# run VSCode or VSCodium from the current shell environment +code . +# Run or Debug using VSCode and VSCodium using the linux Launch configuration. +``` +![Debug using VSCode and VSCodium](./images/StartingDebuggingInVSCode.png) + +You can also Build and run your application inside the shell. +``` +dotnet build ./Source/LibationAvalonia/LibationAvalonia.csproj -p:TargetFrameworks=net9.0 -p:TargetFramework=net9.0 -p:RuntimeIdentifier=linux-x64 +``` + +--- + +## Notes +- Leaving the current shell environemnt will drop all added dependancies and you will not be able to run or debug the program unless your system has those dependancies defined globally. +- To exit the shell environment voluntarily use `exit` inside the shell. +- Ensure you have no conflicting `nix.conf` or `global.json` that might affect SDK versions or runtime identifiers. +- Keep your `flake.lock` file committed to ensure builds are reproducible for all collaborators. + +--- +## References + +- [Nix Flakes - NixOS Wiki](https://nixos.wiki/wiki/Flakes) +- [Nix.dev - Introduction to Nix flakes](https://nix.dev/manual/nix/2.28/command-ref/new-cli/nix3-flake-init) +- [Nix-shell Manual](https://nixos.org/manual/nix/stable/command-ref/nix-shell.html) diff --git a/Documentation/images/StartingDebuggingInVSCode.png b/Documentation/images/StartingDebuggingInVSCode.png new file mode 100644 index 00000000..1e43b2b5 Binary files /dev/null and b/Documentation/images/StartingDebuggingInVSCode.png differ diff --git a/flake.lock b/flake.lock new file mode 100644 index 00000000..1e5b5141 --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1749794982, + "narHash": "sha256-Kh9K4taXbVuaLC0IL+9HcfvxsSUx8dPB5s5weJcc9pc=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "ee930f9755f58096ac6e8ca94a1887e0534e2d81", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 00000000..7de6fbf6 --- /dev/null +++ b/flake.nix @@ -0,0 +1,16 @@ +{ + description = + "Generic flake to run shell.nix with a locked nixos pkgs version. If you have + flakes enabled just run 'nix develop' in the same folder as flake.nix"; + inputs = { + #// use your preferred nixpkgs version + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + }; + + outputs = { nixpkgs, ... }: + let + pkgs = nixpkgs.legacyPackages.x86_64-linux; + in { + devShells.x86_64-linux.default = import ./shell.nix {inherit pkgs;}; + }; +} \ No newline at end of file diff --git a/shell.nix b/shell.nix new file mode 100644 index 00000000..b53c8bf3 --- /dev/null +++ b/shell.nix @@ -0,0 +1,19 @@ +{pkgs ? import {}}: let + libPath = with pkgs; + lib.makeLibraryPath [ + # load external libraries that you need in your dotnet project here + xorg.libX11 + xorg.libICE + xorg.libSM + libGL + fontconfig + ]; +in + pkgs.mkShell { + buildInputs = with pkgs; [ + dotnet-sdk_9 + ]; + + DOTNET_ROOT = "${pkgs.dotnet-sdk_9}/share/dotnet"; + LD_LIBRARY_PATH =libPath; + }