Skip to content

Add Flake Dev Environment and Optional Package Output #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
if ! has nix_direnv_version || ! nix_direnv_version 3.0.6; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.6/direnvrc" "sha256-zelF0vLbEl5uaqrfIzbgNzJWGmLzCmYAkInj/LNxvKs="
fi

watch_file flake.nix
watch_file flake.lock
if ! use flake . --no-pure-eval
then
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
fi
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Thumbs.db
# Binary output directory
/bin/
/dist/
.direnv
result

# Local environment variables
.env
Expand Down
48 changes: 48 additions & 0 deletions flake.lock

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

145 changes: 145 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
{
description = "OpenCode - Terminal-based AI assistant for software development";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
treefmt-nix.url = "github:numtide/treefmt-nix";
treefmt-nix.inputs.nixpkgs.follows = "nixpkgs";
};

outputs = {
self,
nixpkgs,
treefmt-nix,
...
}: let
supportedSystems = [
"x86_64-linux"
"x86_64-darwin"
"aarch64-linux"
"aarch64-darwin"
];
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
in {
packages = forAllSystems (system: let
pkgs = import nixpkgs {
inherit system;
};
in {
default = pkgs.buildGo124Module {
pname = "opencode";
version = "0.1.0";
src = self;
vendorHash = "sha256-Kcwd8deHug7BPDzmbdFqEfoArpXJb1JtBKuk+drdohM=";
doCheck = false;

ldflags = ["-s" "-w"];

meta = with pkgs.lib; {
description = "OpenCode - Terminal-based AI assistant for software development";
homepage = "https://github.com/opencode-ai/opencode";
license = licenses.mit;
mainProgram = "opencode";
};
};
});

devShells = forAllSystems (system: let
pkgs = import nixpkgs {
inherit system;
};

scripts = {
gen = {
exec = ''go generate ./...'';
description = "Run code generation";
};
lint = {
exec = ''golangci-lint run'';
description = "Run Linting Steps for go files";
};
build = {
exec = ''go build -o opencode .'';
description = "Build the OpenCode CLI";
};
run = {
exec = ''go run .'';
description = "Run the OpenCode CLI";
};
tests = {
exec = ''go test ./...'';
description = "Run all go tests";
};
};

scriptPackages =
pkgs.lib.mapAttrs
(
name: script:
pkgs.writeShellApplication {
inherit name;
text = script.exec;
runtimeInputs = script.deps or [];
}
)
scripts;

buildWithSpecificGo = pkg: pkg.override {buildGoModule = pkgs.buildGo124Module;};
in {
default = pkgs.mkShell {
name = "opencode-dev";

packages = with pkgs;
[
# Nix tools
alejandra
nixd
statix
deadnix

# Go Tools
go_1_24
air
golangci-lint
gopls
(buildWithSpecificGo revive)
(buildWithSpecificGo golines)
(buildWithSpecificGo golangci-lint-langserver)
(buildWithSpecificGo gomarkdoc)
(buildWithSpecificGo gotests)
(buildWithSpecificGo gotools)
(buildWithSpecificGo reftools)
pprof
graphviz
goreleaser
cobra-cli

# CLI development tools
sqlite
]
++ builtins.attrValues scriptPackages;

shellHook = ''
export REPO_ROOT=$(git rev-parse --show-toplevel)
echo "OpenCode development environment loaded"
echo "Run 'build' to build the CLI"
echo "Run 'run' to start the OpenCode CLI"
echo "Run 'tests' to run all tests"
'';
};
});

# Runnable with: > nix fmt
formatter = forAllSystems (system: let
pkgs = nixpkgs.legacyPackages.${system};
treefmtModule = {
projectRootFile = "flake.nix";
programs = {
alejandra.enable = true; # Nix formatter
gofmt.enable = true; # Go formatter
};
};
in
treefmt-nix.lib.mkWrapper pkgs treefmtModule);
};
}