commit 8bfb2256f8374dbb9be1aa6e9c3849de72c3088d Author: EatThePooh Date: Wed Dec 3 18:48:46 2025 +0700 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68210e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.emacs* +result +.oc-local \ No newline at end of file diff --git a/example/AGENTS.md b/example/AGENTS.md new file mode 100644 index 0000000..4d6aca2 --- /dev/null +++ b/example/AGENTS.md @@ -0,0 +1 @@ +If you read this, say "I'm running in a container like a good boy should" diff --git a/example/flake.lock b/example/flake.lock new file mode 100644 index 0000000..e0ef3ef --- /dev/null +++ b/example/flake.lock @@ -0,0 +1,123 @@ +{ + "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1763759067, + "narHash": "sha256-LlLt2Jo/gMNYAwOgdRQBrsRoOz7BPRkzvNaI/fzXi2Q=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "2cccadc7357c0ba201788ae99c4dfa90728ef5e0", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, + "flake-parts_2": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib_2" + }, + "locked": { + "lastModified": 1763759067, + "narHash": "sha256-LlLt2Jo/gMNYAwOgdRQBrsRoOz7BPRkzvNaI/fzXi2Q=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "2cccadc7357c0ba201788ae99c4dfa90728ef5e0", + "type": "github" + }, + "original": { + "id": "flake-parts", + "type": "indirect" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1764517877, + "narHash": "sha256-pp3uT4hHijIC8JUK5MEqeAWmParJrgBVzHLNfJDZxg4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "2d293cbfa5a793b4c50d17c05ef9e385b90edf6c", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1761765539, + "narHash": "sha256-b0yj6kfvO8ApcSE+QmA6mUfu8IYG6/uU28OFn4PaC8M=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "719359f4562934ae99f5443f20aa06c2ffff91fc", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "nixpkgs-lib_2": { + "locked": { + "lastModified": 1761765539, + "narHash": "sha256-b0yj6kfvO8ApcSE+QmA6mUfu8IYG6/uU28OFn4PaC8M=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "719359f4562934ae99f5443f20aa06c2ffff91fc", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1764642553, + "narHash": "sha256-mvbFFzVBhVK1FjyPHZGMAKpNiqkr7k++xIwy+p/NQvA=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "f720de59066162ee879adcc8c79e15c51fe6bfb4", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "ocd": { + "inputs": { + "flake-parts": "flake-parts_2", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "path": "../", + "type": "path" + }, + "original": { + "path": "../", + "type": "path" + }, + "parent": [] + }, + "root": { + "inputs": { + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs", + "ocd": "ocd" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/example/flake.nix b/example/flake.nix new file mode 100644 index 0000000..b8d9b8f --- /dev/null +++ b/example/flake.nix @@ -0,0 +1,23 @@ +{ + description = "Usage example"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; + # replace with "github:voleum-org/ocd" + ocd.url = "path:../"; + }; + + outputs = inputs@{ flake-parts, ocd, ...}: + flake-parts.lib.mkFlake { inherit inputs; } { + imports = [ + ocd.flakeModule + ]; + + systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + + perSystem = { config, pkgs, ... }: { + ocd.enable = true; + }; + }; +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..5999137 --- /dev/null +++ b/flake.lock @@ -0,0 +1,7 @@ +{ + "nodes": { + "root": {} + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..49330f6 --- /dev/null +++ b/flake.nix @@ -0,0 +1,52 @@ +{ + description = "Provides containerized opencode"; + + outputs = inputs@{ ... }: { + flakeModule = { self, lib, flake-parts-lib, ... }: + let + inherit (flake-parts-lib) mkPerSystemOption; + inherit (lib) mkOption mkEnableOption types; + in { + options.perSystem = mkPerSystemOption ({ pkgs, system, ...}: { + options.ocd = { + enable = mkEnableOption "Containerized opencode"; + ocUrl = mkOption { + type = types.str; + default = "https://registry.npmjs.org/opencode-linux-x64/-/opencode-linux-x64-1.0.129.tgz"; + description = "URL of the opencode tarball"; + }; + ocSha256 = mkOption { + type = types.str; + default = "sha256:0md8iq7fk6nr3qfbljzyghnz1v0hqqjsmdwp1icql7l1a8nfa09z"; + description = "SHA256 hash of the opencode tarball"; + }; + withPackages = mkOption { + type = types.functionTo (types.listOf types.package); + default = p: []; + description = "Extra packages to include in the container"; + example = lib.literalExpression "p: [ p.tmux ]"; + }; + }; + }); + + config = { + perSystem = { config, pkgs, system, ...}: + lib.mkIf config.ocd.enable { + devShells.default = pkgs.mkShell { + shellHook = '' + set -v + alias ocd-init='$(nix build .#ocd --print-out-paths) | docker load' + alias ocd-start='docker run -it --rm -v $(pwd):/workspace -v $(pwd)/.oc-local:/root/.local/share/opencode --network host ocd:latest' + set +v + echo 'ocd-init needs to be ran once (or on image change)' + ''; + }; + + packages.ocd = pkgs.callPackage ./image.nix { + inherit (config.ocd) withPackages ocUrl ocSha256; + }; + }; + }; + }; + }; +} diff --git a/image.nix b/image.nix new file mode 100644 index 0000000..608798c --- /dev/null +++ b/image.nix @@ -0,0 +1,30 @@ +{ pkgs ? import {}, withPackages, ocUrl, ocSha256 }: + +let + + opencode = builtins.fetchTarball { + url = ocUrl; + sha256 = ocSha256; + }; +in +pkgs.dockerTools.streamLayeredImage { + name = "ocd"; + tag = "latest"; + + contents = [ + pkgs.glibc + opencode + ] ++ (withPackages pkgs); + + fromImage = pkgs.dockerTools.pullImage { + imageName = "alpine"; + imageDigest = "sha256:85f2b723e106c34644cd5851d7e81ee87da98ac54672b29947c052a45d31dc2f"; + sha256 = "sha256-7ZZq7Hzgpcf7gvUrfC84SW4FrN2FwwF5+kYgWKYJQuQ="; + }; + + config = { + Cmd = [ "/bin/sh" ]; + WorkingDir = "/workspace"; + Volumes = { "/workspace" = {}; "/root/.local/share/opencode" = {}; }; + }; +}