Skip to content

Commit 70a008b

Browse files
authored
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 70a008b

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-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: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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 = [
37+
"-s"
38+
"-w"
39+
];
40+
41+
meta = with pkgs.lib; {
42+
description = "OpenCode - Terminal-based AI assistant for software development";
43+
homepage = "https://github.com/opencode-ai/opencode";
44+
license = licenses.mit;
45+
};
46+
};
47+
});
48+
49+
devShells = forAllSystems (system: let
50+
pkgs = import nixpkgs {
51+
inherit system;
52+
};
53+
54+
scripts = {
55+
gen = {
56+
exec = ''go generate ./...'';
57+
description = "Run code generation";
58+
};
59+
lint = {
60+
exec = ''golangci-lint run'';
61+
description = "Run Linting Steps for go files";
62+
};
63+
build = {
64+
exec = ''go build -o opencode .'';
65+
description = "Build the OpenCode CLI";
66+
};
67+
run = {
68+
exec = ''go run .'';
69+
description = "Run the OpenCode CLI";
70+
};
71+
tests = {
72+
exec = ''go test ./...'';
73+
description = "Run all go tests";
74+
};
75+
};
76+
77+
scriptPackages =
78+
pkgs.lib.mapAttrs
79+
(
80+
name: script:
81+
pkgs.writeShellApplication {
82+
inherit name;
83+
text = script.exec;
84+
runtimeInputs = script.deps or [];
85+
}
86+
)
87+
scripts;
88+
89+
buildWithSpecificGo = pkg: pkg.override {buildGoModule = pkgs.buildGo124Module;};
90+
in {
91+
default = pkgs.mkShell {
92+
name = "opencode-dev";
93+
94+
packages = with pkgs;
95+
[
96+
# Nix tools
97+
alejandra
98+
nixd
99+
statix
100+
deadnix
101+
102+
# Go Tools
103+
go_1_24
104+
air
105+
golangci-lint
106+
gopls
107+
(buildWithSpecificGo revive)
108+
(buildWithSpecificGo golines)
109+
(buildWithSpecificGo golangci-lint-langserver)
110+
(buildWithSpecificGo gomarkdoc)
111+
(buildWithSpecificGo gotests)
112+
(buildWithSpecificGo gotools)
113+
(buildWithSpecificGo reftools)
114+
pprof
115+
graphviz
116+
goreleaser
117+
cobra-cli
118+
119+
# CLI development tools
120+
sqlite
121+
]
122+
++ builtins.attrValues scriptPackages;
123+
124+
shellHook = ''
125+
export REPO_ROOT=$(git rev-parse --show-toplevel)
126+
echo "OpenCode development environment loaded"
127+
echo "Run 'build' to build the CLI"
128+
echo "Run 'run' to start the OpenCode CLI"
129+
echo "Run 'tests' to run all tests"
130+
'';
131+
};
132+
});
133+
134+
# Runnable with: > nix fmt
135+
formatter = forAllSystems (system: let
136+
pkgs = nixpkgs.legacyPackages.${system};
137+
treefmtModule = {
138+
projectRootFile = "flake.nix";
139+
programs = {
140+
alejandra.enable = true; # Nix formatter
141+
gofmt.enable = true; # Go formatter
142+
};
143+
};
144+
in
145+
treefmt-nix.lib.mkWrapper pkgs treefmtModule);
146+
};
147+
}

0 commit comments

Comments
 (0)