Skip to content

Commit

Permalink
Nix flake (#4)
Browse files Browse the repository at this point in the history
* Add Nix flake

* Add Nix flake build instructions to README
  • Loading branch information
ramiuslr authored Dec 16, 2024
1 parent 868b8b6 commit 93edc5e
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ go.work

# Goreleaser
dist/

# Nix build output
result
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,42 @@ docker pull ghcr.io/marevers/pleasant-cli:latest
docker pull ghcr.io/marevers/pleasant-cli:<version>
```

## Build

### Nix

To build the project as a *Nix flake* (ensure you have *Nix* installed with flakes enabled):
```bash
nix build
```
This creates a `result` symlink pointing to the built package in your *Nix* store.

To run it directly:
```bash
nix run
```

To enter a development environment with all necessary dependencies:
```bash
nix develop
```

To install it in your current profile:
```bash
nix profile install
```

You may also want to update the locked dependencies in `flake.lock`:
```bash
nix flake update
```

As *Nix* uses hashes to ensure reproducibility between builds, you may need to update the vendor hash in `flake.nix` when Go dependencies change (for example after `go get -u`).

To do so, run `nix build` as usual: this will fail giving the expected new hash that you can use to update the `vendorHash` variable in `flake.nix`. Once updated, the rebuild should work as expected.

For more information about using flakes in *Nix* and *NixOS* environments please have a look at the [documentation](https://nixos.wiki/wiki/flakes).

## Roadmap

**Clipboard support**
Expand Down
61 changes: 61 additions & 0 deletions flake.lock

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

43 changes: 43 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
description = "A command line interface for Pleasant Password Server";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};

outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};

# Version extraction from source code
rootGoContent = builtins.readFile ./cmd/root.go;
versionMatch = builtins.match
".*var version = \"([^\"]*)\".*" rootGoContent;
version =
if versionMatch == null
then throw "Version not found in cmd/root.go"
else builtins.head versionMatch;

in
{
packages.default = pkgs.buildGoModule {
pname = "pleasant-cli";
inherit version;
src = ./.;
vendorHash = "sha256-mRAlpDUg+2O/ShhxNVGGfK8YUJvWcR/ojossgZ7rCEY=";
};

apps.default = flake-utils.lib.mkApp {
drv = self.packages.${system}.default;
};

devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
go
];
};
}
);
}

0 comments on commit 93edc5e

Please sign in to comment.