Skip to content

Commit 8ffcddb

Browse files
committed
Add Flake Dev Environment an Optional Package Output
Added a flake.nix and lock file containing pinned dependencies for working on the project.
1 parent 23763fb commit 8ffcddb

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed

.envrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
if ! has nix_direnv_version || ! nix_direnv_version 3.0.6; then
2+
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.6/direnvrc" "sha256-zelF0vLbEl5uaqrfIzbgNzJWGmLzCmYAkInj/LNxvKs="
3+
fi
4+
5+
watch_file flake.nix
6+
watch_file flake.lock
7+
if ! use flake . --no-pure-eval
8+
then
9+
echo "devenv could not be built. The devenv environment was not loaded. Make the necessary changes to flake.nix and hit enter to try again." >&2
10+
fi

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Thumbs.db
3636
# Binary output directory
3737
/bin/
3838
/dist/
39+
.direnv
40+
result
3941

4042
# Local environment variables
4143
.env

flake.lock

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
{
2+
description = "OpenCode - Terminal-based AI assistant for software development";
3+
4+
inputs = {
5+
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
6+
treefmt-nix.url = "github:numtide/treefmt-nix";
7+
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
8+
};
9+
10+
outputs = {
11+
self,
12+
nixpkgs,
13+
treefmt-nix,
14+
...
15+
}: let
16+
supportedSystems = [
17+
"x86_64-linux"
18+
"x86_64-darwin"
19+
"aarch64-linux"
20+
"aarch64-darwin"
21+
];
22+
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
23+
in {
24+
packages = forAllSystems (system: let
25+
pkgs = import nixpkgs {
26+
inherit system;
27+
};
28+
in {
29+
default = pkgs.buildGo124Module {
30+
pname = "opencode";
31+
version = "0.1.0";
32+
src = self;
33+
vendorHash = "sha256-Kcwd8deHug7BPDzmbdFqEfoArpXJb1JtBKuk+drdohM=";
34+
doCheck = false;
35+
36+
ldflags = ["-s" "-w"];
37+
38+
meta = with pkgs.lib; {
39+
description = "OpenCode - Terminal-based AI assistant for software development";
40+
homepage = "https://github.com/opencode-ai/opencode";
41+
license = licenses.mit;
42+
mainProgram = "opencode";
43+
};
44+
};
45+
});
46+
47+
devShells = forAllSystems (system: let
48+
pkgs = import nixpkgs {
49+
inherit system;
50+
};
51+
52+
scripts = {
53+
gen = {
54+
exec = ''go generate ./...'';
55+
description = "Run code generation";
56+
};
57+
lint = {
58+
exec = ''golangci-lint run'';
59+
description = "Run Linting Steps for go files";
60+
};
61+
build = {
62+
exec = ''go build -o opencode .'';
63+
description = "Build the OpenCode CLI";
64+
};
65+
run = {
66+
exec = ''go run .'';
67+
description = "Run the OpenCode CLI";
68+
};
69+
tests = {
70+
exec = ''go test ./...'';
71+
description = "Run all go tests";
72+
};
73+
};
74+
75+
scriptPackages =
76+
pkgs.lib.mapAttrs
77+
(
78+
name: script:
79+
pkgs.writeShellApplication {
80+
inherit name;
81+
text = script.exec;
82+
runtimeInputs = script.deps or [];
83+
}
84+
)
85+
scripts;
86+
87+
buildWithSpecificGo = pkg: pkg.override {buildGoModule = pkgs.buildGo124Module;};
88+
in {
89+
default = pkgs.mkShell {
90+
name = "opencode-dev";
91+
92+
packages = with pkgs;
93+
[
94+
# Nix tools
95+
alejandra
96+
nixd
97+
statix
98+
deadnix
99+
100+
# Go Tools
101+
go_1_24
102+
air
103+
golangci-lint
104+
gopls
105+
(buildWithSpecificGo revive)
106+
(buildWithSpecificGo golines)
107+
(buildWithSpecificGo golangci-lint-langserver)
108+
(buildWithSpecificGo gomarkdoc)
109+
(buildWithSpecificGo gotests)
110+
(buildWithSpecificGo gotools)
111+
(buildWithSpecificGo reftools)
112+
pprof
113+
graphviz
114+
goreleaser
115+
cobra-cli
116+
117+
# CLI development tools
118+
sqlite
119+
]
120+
++ builtins.attrValues scriptPackages;
121+
122+
shellHook = ''
123+
export REPO_ROOT=$(git rev-parse --show-toplevel)
124+
echo "OpenCode development environment loaded"
125+
echo "Run 'build' to build the CLI"
126+
echo "Run 'run' to start the OpenCode CLI"
127+
echo "Run 'tests' to run all tests"
128+
'';
129+
};
130+
});
131+
132+
# Runnable with: > nix fmt
133+
formatter = forAllSystems (system: let
134+
pkgs = nixpkgs.legacyPackages.${system};
135+
treefmtModule = {
136+
projectRootFile = "flake.nix";
137+
programs = {
138+
alejandra.enable = true; # Nix formatter
139+
gofmt.enable = true; # Go formatter
140+
};
141+
};
142+
in
143+
treefmt-nix.lib.mkWrapper pkgs treefmtModule);
144+
};
145+
}

0 commit comments

Comments
 (0)