{"id":26099261,"url":"https://github.com/LaunchPlatform/nix-playground","last_synced_at":"2025-03-09T16:01:54.156Z","repository":{"id":278866968,"uuid":"936934706","full_name":"LaunchPlatform/nix-playground","owner":"LaunchPlatform","description":"Command line tools for patching nixpkgs package source code easily","archived":false,"fork":false,"pushed_at":"2025-02-22T07:05:36.000Z","size":34,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-22T07:30:45.770Z","etag":null,"topics":["nixpkgs"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LaunchPlatform.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-02-22T00:38:27.000Z","updated_at":"2025-02-22T07:05:39.000Z","dependencies_parsed_at":"2025-02-22T07:40:54.974Z","dependency_job_id":null,"html_url":"https://github.com/LaunchPlatform/nix-playground","commit_stats":null,"previous_names":["launchplatform/nix-playground"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPlatform%2Fnix-playground","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPlatform%2Fnix-playground/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPlatform%2Fnix-playground/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LaunchPlatform%2Fnix-playground/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LaunchPlatform","download_url":"https://codeload.github.com/LaunchPlatform/nix-playground/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242714048,"owners_count":20173583,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nixpkgs"],"created_at":"2025-03-09T16:01:39.294Z","updated_at":"2025-03-09T16:01:54.148Z","avatar_url":"https://github.com/LaunchPlatform.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# nix-playground\n\nThe nix-playground is a command line tool that makes applying patches to the nixpkgs packages much easier.\n\n## Example\n\n```bash\n# checkout libnvidia-container package source code locally\nnp checkout nixpkgs#libnvidia-container\n\n# modify the code\nvim checkout/src/cli/main.c\n\n# build the package with changes you made in the checkout folder and try it out\nnp build\n\n# output the patch for applying on the production environments\nnp patch \u003e bugfix.patch\n\n# clean up the generated files\nnp clean\n```\n\n## Why\nToo often, we are afraid of digging into the upstream code, modifying and patching it because it's a very tedious process.\nJust getting the project to build could take hours.\nThanks to [nixpkgs](https://nixos.org), building open-source software is much easier with a single source tree capable of building from the Linux kernel all the way to a simple utils command-line tool.\nWith nix-playground, now you can easily check out source code from a package, modify it, test it out, and then create patches effortlessly.\n\n## Usage\n\nThis tool assumes you have [nixpkgs](https://nixos.org) with [flake](https://wiki.nixos.org/wiki/Flakes) and [Python](https://www.python.org) \u003e= 3.11 installed on your environment.\nTo install nix-playground, simply run:\n\n```bash\npip install nix-playground\n```\n\nThen, you can use the command line tool `np` (stands for nix-playground).\nFor example, say you need to apply a patch to [libnvidia-container](https://github.com/NVIDIA/libnvidia-container), with the `np` command, you can run:\n\n```bash\nnp checkout nixpkgs#libnvidia-container\n```\n\nIt will check the source code of `libnvidia-container` in the `checkout` folder of the current directory.\nNext, you can modify the code in the `checkout` folder. The tool will track changes you made automatically.\nOnce you're done with the changes and would like to try it out, simply do the following:\n\n```bash\nnp build\n```\n\nIt will build the `libnvidia-container` package with patches from the changes you just made in the `checkout` folder.\nThe result will end up in the `result` folder, just like the `nix-build` command.\nYou can test the build. When you're happy with the result and decide to port the patch file to your production environments, you can run the command:\n\n```bash\nnp patch \u003e bugfix.patch\n```\n\nTo print the patch file contents.\nWith the patch file, you can then apply it on the target package like this:\n\n```nix\nwith import \u003cnixpkgs\u003e {};\n    libnvidia-container.overrideAttrs (oldAttrs: {\n        patches = (lib.attrsets.attrByPath [\"patches\"] [] oldAttrs) ++ [./bugfix.patch];\n    })\n```\n\n### Checkout specific version\n\nOne of the issues we encountered while using nix-playground was the version we patched locally is different from the one running on our production environment.\nTo solve the problem, you can checkout specific version of nixpkgs or flake with the [flake reference URL schema](https://nix.dev/manual/nix/2.24/command-ref/new-cli/nix3-flake#examples).\nFor example, to fix a bug of `libnvidia-container` in the `nixpkgs` repo at commit `11415c7ae8539d6292f2928317ee7a8410b28bb9`, you can run the following\n\n```bash\nnp checkout github:NixOS/nixpkgs/11415c7ae8539d6292f2928317ee7a8410b28bb9#libnvidia-container\n```\n\n## How it works\n\n### Checkout\n\n1. Create `.nix-playground` folder in the current directory\n2. Call `nix derivation show` to output the derivation as a json file at `.nix-playground/drv.json`\n3. Get the `env.src` nix store path from the generated derivation \n4. Run `nix-store --realise` to realise the package and its source derivation with links in the `.nix-playground` folder.\n5. Deep copy the source (from env.src) folder, or untar it to the current directory's `checkout` folder\n6. Init a git repo in the `checkout` folder and commit all the changes\n7. Apply patches from the package as commits in the `checkout` Git repo if there's any\n\n### Build\n\n1. Get the cached diff with git for the `checkout` folder and output the patch file to `.nix-playground/checkout.patch`\n2. Insert patch into the payload from `.nix-playground/drv.json`\n3. Call `nix derivation add` to add the derivation with patch added\n4. Read output patch does not match error, if there's any, patch the derivation payload and jump to step.3 and try again\n5. Realize the patched derivation and build\n\n## Roadmap\n\n- Implement `np shell` (like `nix-shell` but with the patches applied)\n- Support patching nested dependency package\n- Rewrite it with Rust? 🤔\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLaunchPlatform%2Fnix-playground","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FLaunchPlatform%2Fnix-playground","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FLaunchPlatform%2Fnix-playground/lists"}