From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:c151::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id 6GT9I88xUWAMOgAA0tVLHw (envelope-from ) for ; Tue, 16 Mar 2021 22:31:43 +0000 Received: from aspmx2.migadu.com ([2001:41d0:2:c151::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id mFfNH88xUWDzKwAA1q6Kng (envelope-from ) for ; Tue, 16 Mar 2021 22:31:43 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx2.migadu.com (Postfix) with ESMTPS id 918BB13510 for ; Tue, 16 Mar 2021 23:31:42 +0100 (CET) Received: from localhost ([::1]:42166 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lMIE0-0005In-GO for larch@yhetil.org; Tue, 16 Mar 2021 18:31:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46988) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lMIDK-0005Gj-Vg for emacs-orgmode@gnu.org; Tue, 16 Mar 2021 18:30:59 -0400 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]:33713) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lMIDI-0005I2-Ox for emacs-orgmode@gnu.org; Tue, 16 Mar 2021 18:30:58 -0400 Received: by mail-pl1-x630.google.com with SMTP id c16so17663554ply.0 for ; Tue, 16 Mar 2021 15:30:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=references:user-agent:from:to:subject:date:in-reply-to:message-id :mime-version; bh=2LRK2qDqaP6esEV1H/uIooq3913U6TxN8+7dJilYcOM=; b=p1n1m1VzH8eRRpGUuBPw7lV/Nr2rIaT+LGkkvBgiGVGzgSEF1wQEMFugIdqLoa6f3w 9LiFcRtTaAHMiglY1c7AYhUFGr6uWKFuRJyMtYYgo+R0wbo32sAixPr97lokSX5D8cFE vuacHSegSD7AIYu5tHuXlIGjWkbFxcKFRKRzZOEQcWaZfL5cL7CGCdHMznjpIEwcciro 4jfY98jI7URVScVI3Y1LkjG6i40hZbtRqfcQmYFK4UujMhmJLCp666iQc2FHeSS6INfc 4sucrJi+UgEDO4+dIDlaqAXjksSoS1K8udYQ9fyp66OhmGVN/UFefTDXJ7N9YPghFnGn V8oQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:subject:date :in-reply-to:message-id:mime-version; bh=2LRK2qDqaP6esEV1H/uIooq3913U6TxN8+7dJilYcOM=; b=RZZj3JCmQZKxBwXU7mLh6cbmNnrePNWSaQZ9BXYIOEyoEuWHvbvq7LKojodKI7HTOg p+wNjhBICtWmkQVvYS7M1Nif3pP84fjDi3DHFh597buevmr1hXm1X1i1k3j4RZ0siLtt AJK70vGChwELb5F3cs1liBLWP5k2zjuwoa9dab2SoEb8Y1S6r2jK/L19X2n7YdO8v6m4 3v/T3U19NvJZT7NAdi4YourzmPHEYXmYwwo3x5EoVSeByWZ5I9705N9ePt8YMeuvV508 yvehwzXWkfmBXLcqI0vZ28prBNkY94NhBA8JPpSxmVHJFyc6pnicMQNjnOB9yphxAnQY ExWw== X-Gm-Message-State: AOAM533+Fg+H5kzWrkpLsCpf26n+tTx7nV8AmU1lO1GDc/Zx1+cZtjGd 852ScN8Ds34uPr2iLaSeaHWgRUM2sbA= X-Google-Smtp-Source: ABdhPJz6JLnlot+3aArYxwvA+5D0JDC0iyK7EoCPIX+eqoHIEfTZMnj3IobUXqBIkYHWCSMtLkqdJQ== X-Received: by 2002:a17:902:edc6:b029:e4:6dba:415e with SMTP id q6-20020a170902edc6b02900e46dba415emr1437908plk.65.1615933854135; Tue, 16 Mar 2021 15:30:54 -0700 (PDT) Received: from tim-desktop (106-69-70-113.dyn.iinet.net.au. [106.69.70.113]) by smtp.gmail.com with ESMTPSA id 186sm19097573pfb.143.2021.03.16.15.30.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Mar 2021 15:30:53 -0700 (PDT) References: <87a6r5l5bh.fsf@gmail.com> <87ft0x2hbe.fsf@gmail.com> User-agent: mu4e 1.5.10; emacs 27.1.91 From: Tim Cross To: emacs-orgmode@gnu.org Subject: Re: How to get shell source blocks to read my profile? Date: Wed, 17 Mar 2021 07:32:52 +1100 In-reply-to: Message-ID: <87a6r2ybmv.fsf@gmail.com> MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=theophilusx@gmail.com; helo=mail-pl1-x630.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1615933902; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=2LRK2qDqaP6esEV1H/uIooq3913U6TxN8+7dJilYcOM=; b=Wijowhsr0vGKNFTrLtAQWcRxPM5LxCyDbn7wG/chqWPbwdtiPLasOeU0ZfxHlN/CS8Q5ya psIAuC7SdBqlYXOKdin2FYKvYW/wWS28digRonX2j+2vSNOO4BQgDKYsC9Nh8m8MSxjvyB UR8PeVx8glXB8daTm9bTeGik1JO4cvflPGnyAPH67G6lVJWLQy9n0Xet43deBnldNWE/OI Ma6NVQntz29UcP6ZaEXy58MvbDUtutmQSyUuMnxozs6Ob0Mqkypma4AnbOk78POZ0aKzzI XsqtsLM2yMjPz/xu/qBcpESSad5KP/fbJpBjIf+b1Tiw3Xn7iozc6r3tHr4vPQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1615933902; a=rsa-sha256; cv=none; b=aqCSgCOehGvDR4xGvt+MuLKzJ4T/qId1RBAIiBBITXrry0tLZku/ggeF1RWn6v9kczRT10 pdNmrT5n9HSTscBY4meTi+EWzYt9tH0MUs/NNOhql2/Z256zeiaplIzZy4gtaPs7Xd5Lay fdRx/yX86/kp5Fw9FSmLqvMZe18W1pBZ9Wh574M12NBjkxFpt9IOyCxs0oDbnYVxspiNdl lEBk27/BBwe6p8/DVQdyz7bze17+rHPGEyLz+FHcfgVu/SBlHKJPUIzxVXcIxIeYLr6GIx 8lRKU10yzvr3KTwOl/uc1p2v3tY6+gLR5lJRODJN87Gj3o8Dhjh5JsPuvFhCcQ== ARC-Authentication-Results: i=1; aspmx2.migadu.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=p1n1m1Vz; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx2.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Spam-Score: -2.10 Authentication-Results: aspmx2.migadu.com; dkim=pass header.d=gmail.com header.s=20161025 header.b=p1n1m1Vz; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx2.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Migadu-Queue-Id: 918BB13510 X-Spam-Score: -2.10 X-Migadu-Scanner: scn0.migadu.com X-TUID: QCe3llZKiopq George Mauer writes: > I do still wonder what would be the disadvantage of just configuring it to do --login by default and doing all configuration in profile scripts. It would be > unconventional yes, but it would also make dynamic scoping of environment variable effectively opt-in via --noprofile rather than opt-out (which imo is > how it should be). I would assume that uses extra resources or risks improperly handling crashed processes, but I can't find anything to that effect in the > docs The following is long, but may provide some of the context you are looking for to help understand all the moving parts involved here. Some of ti is not 100% accurate, but trying to be 100% accurate would make it longer and would not necessarily help with the mental model needed to understand how this all fits together. Hopefully, this has a reasonable balance between accuracy and simplicity! While there is certainly aspects of shell architecture which are less relevant in a modern computing environment, the way it all works is actually pretty good. It can sometimes help to remember what early Unix environments were like and what the resource constraints were to understand some of the design decisions. A time when memory and disk storage was extremely expensive and in short supply, where CPUs were slower and less capable than those commonly found in a modern washing machine. This was a time where every CPU cycle, every memory bit and every byte of storage had to be justified and used efficiently. The 'login' shell actually has a special purpose and a few subtleties associated with it that may not be obvious at first. For example, the login shell usually records a user's login in a few system files like wtmp and utmp. The idea is that the login shell will be the 'root' of a user's process tree. One of its key roles is to setup the environment which will be inherited by all the other (child) processes which the user initiates. This includes some things which you only want run once and processes which must run as parents to all other processes. A good example is setting up an ssh agent and adding ssh keys to that agent. You only want this to occur once, so you might add this to your .profile (or whatever the shell equivalent is, such as .bash_profile or .zsh_profile). More critical is the requirement that the ssh agent must be run inside a parent process for child processes to be able to use it. This means it needs to be near the 'root' login process to ensure all children are able to take advantage of the service it provides. (Keep in mind that some platforms, like the mac, have created a whole new architecture for managing keys etc and even on Linux, it is rarely necessary to run ssh-agent like we did in the 'old days' as that functionality has been subsumed into desktop environments like gnome) It is also here that you will define static environment variables i.e. those which will not change during the lifetime of the session and which are exported, meaning they are made available in the environments of child processes. Examples are setting up things like PATH, EMAIL etc. Some common environment variables, like USER, HOME etc are done automatically as part of the system wide profile in /etc/profile or the system wide path defined in /etc/paths and /etc/paths.d etc. In addition to these static environment variables are more dynamic ones which change and therefore need to be updated with each new shell process. This can include variables like PWD and CWD which track the parent and current working directory or the PS1 and PS2 variables which contain details about your shell prompt that may include dynamic information such as the current working directory, current date/time or status of the git repository associated with the current working directory. This is essentially the role fulfilled by the 'rc' files, like .bashrc. As these dynamic enironment variables tend to only be relevant for interactive shells, the rc file is only 'sourced' for those shells. When you execute a command, it creates a new process which has an execution environment associated with it (there are some exceptions, such as built-in shell commands that run inside the current process and command 'modifiers' like 'exec' which replace the current process with the command being executed). Some of these processes will be very short lived, executing a single command or possibly run for hours, days, weeks (such as Emacs). This process is a child process of the process which ran/executed the command. You can use the -f (forest) switch to ps to see the relationship between processes. Those processes in turn may execute more child processes (such as when Emacs creates a shell process to perform some task, like run a compilation task, spawn a shell or terminal, etc. The environment each of these processes inherits consists of the exported variables defined in the parent process. This is typically all the exported variables from the login process (as it is the parent of all) and any variables exported by parent processes in the process hierarchy (such as any exported as part of an interactive shell which sourced .bashrc for example) or is defined and exported in a parent (such as part of a shell script which then executes the command which creates the child process. So, in general, you don't want to run all or many of your processes as a login shell. At the very least it is wasting resources and at the worst it could actually cause confusion or break things because it is causing things to be run multiple times which should only be run once. I suspect some of the confusion here is being cause because of the different model approach used by the Mac OS. On the mac, you actually have at least two process trees (I'm not a mac expert, so it is possible there are more than two - in fact I'm pretty sure there are, but for explanation purposes....). On the mac you have your login process tree and your 'dock' (launcher) process tree. The dock process tree is not rooted as the child of a login shell, so there is no sourcing of .profile and it is not an interactive shell, so no .bashrc either. The execution environment associated with processes it spawns (such as when you run a command) only consist of the global system environment setup by the system prfile in /etc/profile, /etc/paths and /etc/paths.d and any environment variables created and exported by the process itself (i.e. using the setenv command in your emacs init.el). Whether emacs is run in terminal mode or in GUI mode has nothing to do with what environment context it has. The context is determined by the environment context of the process parent. So a common way to ensure your .profile environment variables are available in Emacs when it is run from the dock is to use a shell wrapper script which first sources .profile to create and export the variables in .pofile and then run emacs. Often you won't just make it a login shell, but simply source .profile. It is this script which is added to the dock and is executed to start Emacs, ensuring it has your .profile variables set and exported to the child process, Emacs. In turn, any child processes created by Emacs will also include this environment context (such as wehn you open an emacs terminal, run M-x shell or execute org source blocks). The other approach is to just add the variables you need using 'setenv' i your emacs init.el file. Often, there are only a few additional variables you want inside of Emacs and child processes it creates, so adding them to your init.el file is not hard. The one which is most often needed is updates to the PATH variable to add additional directories to search for executable files. You can use setenv to do this or you can use the exec-path package or you can do what I do, which is add these additional paths to /etc/paths.d. I don't bother with the wrapper script on the Mac. There are only a couple of additional variables I need defined, so I set them in my init.el using setenv. -- Tim Cross