{
  "path": "/posts/2023/nix-and-direnv",
  "site": "at://did:plc:mracrip6qu3vw46nbewg44sm/site.standard.publication/self",
  "tags": [
    "partly_ai_authored",
    "gpt4",
    "language_models",
    "python"
  ],
  "$type": "site.standard.document",
  "title": "Nix and direnv",
  "updatedAt": "2023-04-02T23:36:15.000Z",
  "publishedAt": "2023-04-02T23:36:15.000Z",
  "textContent": "I know a little about nix.\nNot a lot.\nI know some things about Python virtual environments, asdf and a few things about package managers.\nI've heard the combo of direnv and nix is fantastic from a number of engineers I trust, but I haven't had the chance to figure out what these tools can really do.\nI could totally ask someone what their setup is, but I decided to ask ChatGPT (gpt-4) to see what would happen.\nThe following is the summary of what I did that worked, as instructed and summarized by the LM, with a bit of editing and commentary by me.\n\nCreating a Python environment with Nix\n\nIn this example, we will create a Python environment with specific packages (requests and beautifulsoup4) using Nix. We will then use this environment to run a Python script that fetches a web page and prints its title.\n\nPython Script\n\nCreate a Python script named web_title.py with the following content:\n\nNix environment\n\nCreate a Nix expression named python-environment.nix with the following content:\n\nThis Nix expression creates a shell environment with the required Python packages.\nThe withPackages function is used to create a custom Python interpreter that includes the specified packages.\n\nRunning the script\n\nTo enter the Nix shell environment, run the following command in your terminal:\n\nInside the nix-shell environment, you can run the web_title.py script with:\n\nThis will fetch the web page at https://nixos.org and print its title.\n\nBy using Nix, we ensure that the Python environment and its dependencies are managed in an isolated and reproducible manner, without affecting the rest of the system or other projects.\n\nAdding direnv to the environment\n\nIf you don't have it already, install direnv from here.\n\nCreate an .envrc file in the same directory as your python-environment.nix file with the following content:\n\nReplace \"your-secret-key-here\" with the actual secret you want to use.\n\nCreate a symbolic link named shell.nix pointing to the python-environment.nix file:\n\nAllow direnv to load the .envrc file in the current directory:\n\nNow, direnv will automatically load the Nix environment when you enter the directory, and unload it when you leave. Additionally, the MY_SECRET_KEY environment variable will be set with the specified value.\n\nYou just have everything there, loaded and isolated when you cd into the project directory.\n\nInside your Python scripts, you can access the environment variable using the os.environ dictionary:\n\nIMPORTANT: To avoid sharing sensitive information, do not commit your .envrc file to version control\n\nWrapping it up\n\nTo tie it all together, run the example above, but with direnv you no longer need to run nix-shell python-environment.nix.\n\nLet's modify the code a little to print the environment variable.\n\nNow run it with just:\n\nOutput:\n\nšŸ‘",
  "canonicalUrl": "https://www.danielcorin.com/posts/2023/nix-and-direnv"
}